﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Linq;

using Microsoft.Office.Tools.Excel;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using System.Text.RegularExpressions;
//线程用到命名空间
using System.Threading;
using System.Data.OleDb;

namespace 料单生成
{
    public partial class Form1 : Form
    {
        //public static Form1 pForm1 = null;

        public Form1()
        {
           // pForm1 = this;
            InitializeComponent();
        }

        static string strPath = @"C:\BOM\OrderOutS.xml";
        static string strID = "";
       //int ProNameColumn = 0;
       //int PrintDateColumn = 1;
       //int BomFileNameColumn  =2;
       //int BomChinaNameColumn =3;
       //int PcbNameColumn  =4;
       //int PcbRevColumn = 5;
       //int PcbSizeColumn = 6;
       //int PcbDateColumn =7;
       //int PcbLayerColumn =8;
       //int PcbNumberColumn =9;
       //int PcbThicknessColumn= 10;
       //int PcbCraftColumn=11;
       //int PcbOutDateColumn=12;
       //int PcbPathColumn=13;
        //14;
        //15;
        //16
        int  IDColumn = 17;


        //创建
        private void button1_Click(object sender, EventArgs e)
        {

            XDocument doc = new XDocument(
                                new XDeclaration("1.0", "utf-8", "yes"),
                                new XElement("OrderOutS",                               //定单输出 数据库
                                new XElement("OrderOut",                                //定单输出 一个定单
                                new XAttribute("ID", "00001"),                           //ID
                                //new XElement("OutID", "00001"),                          //定单序号
                                new XElement("ProName", "L1305"),                       //项目名称
                                new XElement("PrintDate","2013-07-16"),                 //打印日期
                                new XElement("BomFileName", "KZB"),                     //BOM表名
                                new XElement("BomChinaName", "机车信号控制板"),         //BOM中文名
                                new XElement("PcbName", "KZB"),                    //PCB板名
                                new XElement("PcbRev", "V10"),                          //PCB版本
                                new XElement("PcbSize", "150*120"),                     //PCB尺寸
                                new XElement("PcbDate", "2013-07-16"),                  //PCB板内日期
                                new XElement("PcbLayer", "4"),                          //PCB层数
                                new XElement("PcbNumber", "10"),                        //PCB下单数量
                                new XElement("PcbThickness", "1.6MM"),                  //PCB厚度
                                new XElement("PcbCraft", "无铅喷纯锡"),                 //PCB工艺
                                new XElement("PcbOutDate", "2014-05-06"),               //PCB加工日期
                                new XElement("PcbPath", @"C:\Test.pcb"),                          //PCB路径
                                new XElement("PcbSoft", "protel99se"),                   //PCB设计软件
                                new XElement("ReserveOne", "备用1"),                   //改为料单料路径
                                new XElement("ReserveTwo", "备用2")                   //PCB设计软件
                                )
                                )
                              );

            doc.Save(strPath);
            //groupBox1.Enabled = false;
            getXmlInfo();
            button1.Enabled = false;

        }

        #region 将XML文件内容绑定到DataGridView控件
        /// <summary>
        /// 将XML文件内容绑定到DataGridView控件
        /// </summary>
        private void getXmlInfo()
        {
            DataSet myds = new DataSet();
            myds.ReadXml(strPath);
            //myds.WriteXml();
            if (myds.Tables.Count > 0)
            {
                dataGridView1.DataSource = myds.Tables[0];
            }
            else
            {
                dataGridView1.DataSource = null;
            }            
        }
        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Width = 982;
            this.Height = 615;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;

            if (File.Exists(@"C:\BOM\OUT\OutOrderOutTitle.xml"))
            {
                DataSet myds = new DataSet();
                myds.ReadXml(@"C:\BOM\OUT\OutOrderOutTitle.xml");
                //myds.WriteXml();
                if (myds.Tables.Count > 0)
                {
                    dataGridView1.DataSource = myds.Tables[0];
                    for (int rows = 0; rows < dataGridView1.Rows.Count - 1; rows++)
                    {
                        comboBox1.Items.Add(dataGridView1.Rows[rows].Cells[7].Value.ToString());
                    }
                }
                else
                {
                    dataGridView1.DataSource = null;
                }
            }


            if (File.Exists(strPath))
            {
                button1.Enabled = false; //
                getXmlInfo();
            }


        }

        //料单配置文件保存
        public static string[] GlobalSingleBomConfig = new string[20];

        bool SingleBomConfig = false;

        // 双击DATAgrid
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                strID = dataGridView1.Rows[e.RowIndex].Cells[IDColumn].Value.ToString();

                XElement xe = XElement.Load(strPath);
                IEnumerable<XElement> elements = from PInfo in xe.Elements("OrderOut")
                                                 where PInfo.Attribute("ID").Value == strID
                                                 select PInfo;
                foreach (XElement element in elements)
                {

                    textBoxProName.Text = element.Element("ProName").Value;
                    dateTimePickerPrintDate.Text = element.Element("PrintDate").Value;
                    textBoxBomFileName.Text = element.Element("BomFileName").Value;
                    textBoxBomChinaName.Text = element.Element("BomChinaName").Value;
                    textBoxPcbName.Text = element.Element("PcbName").Value;
                    comboBoxPcbRev.Text = element.Element("PcbRev").Value;
                    textBoxPcbSize.Text = element.Element("PcbSize").Value;
                    dateTimePickerPcbDate.Text = element.Element("PcbDate").Value;
                    comboBoxPcbLayer.Text = element.Element("PcbLayer").Value;
                    textBoxPcbNumber.Text = element.Element("PcbNumber").Value;
                    comboBoxPcbThickness.Text = element.Element("PcbThickness").Value;
                    comboBoxPcbCraft.Text = element.Element("PcbCraft").Value;
                    dateTimePickerPcbOutDate.Text = element.Element("PcbOutDate").Value;
                    textBoxPcbPath.Text = element.Element("PcbPath").Value;
                    PcbSoft.Text = element.Element("PcbSoft").Value;
                    BomPath.Text = element.Element("ReserveOne").Value;
                    RecText.Text = element.Element("ReserveTwo").Value;

                    textBox1.Text = "可选";
                    //只有在这种状太才能更改PCB路径
                    SingleBomConfig = true;
                    //PCB路径
                    button6.Enabled = true;

                    GlobalSingleBomConfig[0] = element.Element("ProName").Value;
                    GlobalSingleBomConfig[1] = element.Element("PrintDate").Value;
                    GlobalSingleBomConfig[2] = element.Element("BomFileName").Value;
                    GlobalSingleBomConfig[3] = element.Element("BomChinaName").Value;
                    GlobalSingleBomConfig[4] = element.Element("PcbName").Value;
                    GlobalSingleBomConfig[5] = element.Element("PcbRev").Value;
                    GlobalSingleBomConfig[6] = element.Element("PcbSize").Value;
                    GlobalSingleBomConfig[7] = element.Element("PcbDate").Value;
                    GlobalSingleBomConfig[8] = element.Element("PcbLayer").Value;
                    GlobalSingleBomConfig[9] = element.Element("PcbNumber").Value;
                    GlobalSingleBomConfig[10] = element.Element("PcbThickness").Value;
                    GlobalSingleBomConfig[11] = element.Element("PcbCraft").Value;
                    GlobalSingleBomConfig[12] = element.Element("PcbOutDate").Value;
                    GlobalSingleBomConfig[13] = element.Element("PcbPath").Value;
                    GlobalSingleBomConfig[14] = element.Element("PcbSoft").Value;
                    GlobalSingleBomConfig[15] = element.Element("ReserveOne").Value;
                    GlobalSingleBomConfig[16] = element.Element("ReserveTwo").Value;

                } 
            }           
        }

        


        public void findlrder()
        {
            //存储要更新的所有ID号的内容
            List<string> updateidList = new List<string>();     

            XElement xes = XElement.Load(@"C:\BOM\OUT\OutOrderOutS.xml");//加载xml文件
                //查询指定名称的元素
                var elements = from element in xes.Elements("OrderOut")
                               where (element.Element("ProName").Value == GlobalSingleBomConfig[0]) && (element.Element("BomFileName").Value == GlobalSingleBomConfig[2]) && (element.Element("PcbRev").Value == GlobalSingleBomConfig[5])
                               select new
                               {
                                   定单ID = element.Element("OutID").Value,
                                   项目名称 = element.Element("ProName").Value,
                                   打印日期 = element.Element("PrintDate").Value,
                                   BOM表名 = element.Element("BomFileName").Value,
                                   BOM中文名 = element.Element("BomChinaName").Value,
                                   PCB板名 = element.Element("PcbName").Value,
                                   PCB版本 = element.Element("PcbRev").Value,
                                   PCB尺寸 = element.Element("PcbSize").Value,
                                   PCB板内日期 = element.Element("PcbDate").Value,
                                   PCB层数 = element.Element("PcbLayer").Value,
                                   PCB下单数量 = element.Element("PcbNumber").Value,
                                   PCB厚度 = element.Element("PcbThickness").Value,
                                   PCB工艺 = element.Element("PcbCraft").Value,
                                   PCB加工日期 = element.Element("PcbOutDate").Value,
                                   PCB路径 = element.Element("PcbPath").Value,
                                   软件版本 = element.Element("PcbSoft").Value,
                                   BOM路径 = element.Element("ReserveOne").Value,
                                   更改记录路径 = element.Element("ReserveTwo").Value,
                                   ID = element.Attribute("ID").Value,
                               };

                if (elements.Count() > 0)
                {
                    dataGridView1.DataSource = elements.ToList();
                    updateidList.Clear();
                    int ist = dataGridView1.Rows.Count;
                    for (int rows = 0; rows < ist; rows++)
                    {
                        updateidList.Add(dataGridView1.Rows[rows].Cells[0].Value.ToString());
                        updateidList.Add(dataGridView1.Rows[rows].Cells[18].Value.ToString());
                    }
                }


                for (int i = 0; i < updateidList.Count;)
                {
                    //更改已下过单的索引内容
                    XElement xeD = XElement.Load(@"C:\BOM\OUT\OutOrderOutS.xml");
                    IEnumerable<XElement> elementsD = from element in xeD.Elements("OrderOut")
                                                      where element.Attribute("ID").Value == updateidList[i + 1]
                                                      select element;
                    //定单ID = element.Element("OutID").Value,
                    if (elementsD.Count() > 0)
                    {
                        XElement newXE = elementsD.First();
                        //newXE.SetAttributeValue("ID", strID);
                        newXE.ReplaceNodes(
                                    new XElement("OutID", updateidList[i]),                        //项目名称
                                    new XElement("ProName", textBoxProName.Text),                      //项目名称
                                    new XElement("PrintDate", dateTimePickerPrintDate.Text),           //打印日期
                                    new XElement("BomFileName", textBoxBomFileName.Text),              //BOM表名
                                    new XElement("BomChinaName", textBoxBomChinaName.Text),            //BOM中文名
                                    new XElement("PcbName", textBoxPcbName.Text),                      //PCB板名
                                    new XElement("PcbRev", comboBoxPcbRev.Text),                       //PCB版本
                                    new XElement("PcbSize", textBoxPcbSize.Text),                      //PCB尺寸
                                    new XElement("PcbDate", dateTimePickerPcbDate.Text),               //PCB板内日期
                                    new XElement("PcbLayer", comboBoxPcbLayer.Text),                   //PCB层数
                                    new XElement("PcbNumber", textBoxPcbNumber.Text),                 //PCB下单数量
                                    new XElement("PcbThickness", comboBoxPcbThickness.Text),            //PCB厚度
                                    new XElement("PcbCraft", comboBoxPcbCraft.Text),                   //PCB工艺
                                    new XElement("PcbOutDate", dateTimePickerPcbOutDate.Text),         //PCB加工日期
                                    new XElement("PcbPath", textBoxPcbPath.Text),                      //PCB路径
                                    new XElement("PcbSoft", PcbSoft.Text),                              //PCB设计软件
                                    new XElement("ReserveOne", BomPath.Text),                           //bom料单路径
                                    new XElement("ReserveTwo", RecText.Text)                               //PCB设计软件
                            );
                    }
                    xeD.Save(@"C:\BOM\OUT\OutOrderOutS.xml");
                    i = i + 2;
                }               
        }




        //修改内容
        private void button2_Click(object sender, EventArgs e)
        {
            bool pcbrev = false, pcbdate = false,EditFlag=false;
            string oldname="",TempPath = "", tBomstr = "", tBomstrnew="";

            int count = 1;

            string tempstr = "更改项为：\n", tempstrbeizhu = "更改原因：配置表更改";
            if (textBox1.Text != "可选")
            {
                tempstrbeizhu = tempstrbeizhu + textBox1.Text;
            }

            //PCB板本
            if (comboBoxPcbRev.Text != GlobalSingleBomConfig[5])
            {
                pcbrev = true; EditFlag = true;
                tempstr = tempstr + count + "、PCB版本从   " + GlobalSingleBomConfig[5] + "   ->    " + comboBoxPcbRev.Text + "\n"; count++;
            }
            //板内日期
            if (Convert.ToDateTime(dateTimePickerPcbDate.Text).ToString("yyyy-MM-dd") != GlobalSingleBomConfig[7])
            {
                pcbdate = true; EditFlag = true;
                tempstr = tempstr + count + "、PCB日期从   " + GlobalSingleBomConfig[7] + "   ->    " + dateTimePickerPcbDate.Text + "\n"; count++;
            }
            if (textBoxBomChinaName.Text != GlobalSingleBomConfig[3])
            {
                EditFlag = true;
                tempstr = tempstr + count + "、PCB中文名从  " + GlobalSingleBomConfig[3] + "   ->    " + textBoxBomChinaName.Text + "\n"; count++;
            }

            if (textBoxPcbSize.Text != GlobalSingleBomConfig[6])
            {
                EditFlag = true;
                tempstr = tempstr + count + "、PCB尺寸从   " + GlobalSingleBomConfig[6] + "   ->    " + textBoxPcbSize.Text + "\n"; count++;
            }

            if (comboBoxPcbLayer.Text != GlobalSingleBomConfig[8])
            {
                EditFlag = true;
                tempstr = tempstr + count + "、PCB板层数从  " + GlobalSingleBomConfig[8] + "   ->    " + comboBoxPcbLayer.Text + "\n"; count++;
            }

            if (textBoxPcbNumber.Text != GlobalSingleBomConfig[9])
            {
                EditFlag = true;
                tempstr = tempstr + count + "、PCB下单数量从  " + GlobalSingleBomConfig[9] + "   ->    " + textBoxPcbNumber.Text + "\n"; count++;
            }

            if (comboBoxPcbThickness.Text != GlobalSingleBomConfig[10])
            {
                EditFlag = true;
                tempstr = tempstr + count + "、PCB厚度从   " + GlobalSingleBomConfig[10] + "   ->    " + comboBoxPcbThickness.Text + "\n"; count++;
            }

            if (comboBoxPcbCraft.Text != GlobalSingleBomConfig[11])
            {
                EditFlag = true;
                tempstr = tempstr + count + "、PCB工艺从   " + GlobalSingleBomConfig[11] + "   ->    " + comboBoxPcbCraft.Text + "\n"; count++;
            }

            if (Convert.ToDateTime(dateTimePickerPcbOutDate.Text).ToString("yyyy-MM-dd") != GlobalSingleBomConfig[12])
            {
                EditFlag = true;
                tempstr = tempstr + count + "、PCB加工日期从 " + GlobalSingleBomConfig[12] + "   ->    " + dateTimePickerPcbOutDate.Text + "\n"; count++;
            }

            if (PcbSoft.Text != GlobalSingleBomConfig[14])
            {
                EditFlag = true;
                tempstr = tempstr + count + "、PCB设计软件从 " + GlobalSingleBomConfig[14] + "   ->    " + PcbSoft.Text + "\n"; count++;
            }

            if ((strID != "") && EditFlag && (MessageBox.Show(tempstr, "是否确定更改", MessageBoxButtons.OKCancel) == DialogResult.OK))
            {
                //两个中有一个即要更改文件名
                if (pcbrev || pcbdate)
                {
                    //更改文件名

                    //BOM表目录
                    oldname = BomPath.Text;
                    TempPath = BomPath.Text;                    
                    //获取旧文件名
                    tBomstr = TempPath.Substring(TempPath.LastIndexOf("\\") + 1, TempPath.LastIndexOf(".") - (TempPath.LastIndexOf("\\") + 1));
                    //设置新的文件名  项目名称                   BOM表名字                 版本
                    tBomstrnew = textBoxProName.Text + textBoxBomFileName.Text + comboBoxPcbRev.Text + Convert.ToDateTime(dateTimePickerPcbDate.Text).ToString("yyyy-MM-dd");
                    //替换旧的文件名字
                    TempPath = TempPath.Replace(tBomstr, tBomstrnew);
                    BomPath.Text = TempPath;                    

                    if (File.Exists(@oldname))
                    {
                        File.Move(@oldname, @TempPath);
                        //删除旧文件
                        File.Delete(@oldname);
                    }


                    //PCB目录文件名
                    oldname = textBoxPcbPath.Text;
                    TempPath = textBoxPcbPath.Text;
                    //获取旧文件名
                    tBomstr = TempPath.Substring(TempPath.LastIndexOf("\\") + 1, TempPath.LastIndexOf(".") - (TempPath.LastIndexOf("\\") + 1));
                    //设置新的文件名  项目名称                   BOM表名字                 版本
                    tBomstrnew = textBoxProName.Text + textBoxBomFileName.Text + comboBoxPcbRev.Text + Convert.ToDateTime(dateTimePickerPcbDate.Text).ToString("yyyy-MM-dd");
                    //替换旧的文件名字
                    TempPath = TempPath.Replace(tBomstr, tBomstrnew);
                    textBoxPcbPath.Text = TempPath;
                    if (File.Exists(@oldname))
                    {
                        File.Move(@oldname, TempPath);
                        //删除旧文件
                        File.Delete(@oldname);
                    }

                    //更改记录目录文件名
                    oldname = RecText.Text;
                    TempPath = RecText.Text;
                    //获取旧文件名
                    tBomstr = TempPath.Substring(TempPath.LastIndexOf("\\") + 1, TempPath.LastIndexOf(".") - (TempPath.LastIndexOf("\\") + 1));
                    //设置新的文件名  项目名称                   BOM表名字                 版本
                    tBomstrnew = textBoxProName.Text + textBoxBomFileName.Text + comboBoxPcbRev.Text + Convert.ToDateTime(dateTimePickerPcbDate.Text).ToString("yyyy-MM-dd") + "更改记录";
                    //替换旧的文件名字
                    TempPath = TempPath.Replace(tBomstr, tBomstrnew);
                    RecText.Text = TempPath;
                    if (File.Exists(@oldname))
                    {
                        File.Move(@oldname, TempPath);
                        //删除旧文件
                        File.Delete(@oldname);
                    }
                }


                //添加更改记录
                XElement xerec = XElement.Load(TempPath);
                IEnumerable<XElement> elements1 = from element in xerec.Elements("ChangeRecord")
                                                  select element;
                //生成新的编号
                string str = (Convert.ToInt32(elements1.Max(element => element.Attribute("ID").Value)) + 1).ToString("00000");

                XElement people = new XElement(
                        "ChangeRecord", new XAttribute("ID", str),
                        new XElement("Content", tempstr),                             //更改内容
                        new XElement("Description", tempstrbeizhu),                   //更改备注说明
                        new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))     //日期
                    );
                xerec.Add(people);
                xerec.Save(TempPath);


                //更改配置表内容
                XElement xe = XElement.Load(strPath);
                IEnumerable<XElement> elements = from element in xe.Elements("OrderOut")
                                                 where element.Attribute("ID").Value == strID
                                                 select element;

                if (elements.Count() > 0)
                {
                    XElement newXE = elements.First();
                    newXE.SetAttributeValue("ID", strID);
                    newXE.ReplaceNodes(
                                new XElement("ProName",textBoxProName.Text),                       //项目名称
                                new XElement("PrintDate", dateTimePickerPrintDate.Text),           //打印日期
                                new XElement("BomFileName", textBoxBomFileName.Text),              //BOM表名
                                new XElement("BomChinaName", textBoxBomChinaName.Text),            //BOM中文名
                                new XElement("PcbName", textBoxPcbName.Text),                      //PCB板名
                                new XElement("PcbRev", comboBoxPcbRev.Text),                       //PCB版本
                                new XElement("PcbSize", textBoxPcbSize.Text),                      //PCB尺寸
                                new XElement("PcbDate", dateTimePickerPcbDate.Text),               //PCB板内日期
                                new XElement("PcbLayer", comboBoxPcbLayer.Text),                   //PCB层数
                                new XElement("PcbNumber",  textBoxPcbNumber.Text),                 //PCB下单数量
                                new XElement("PcbThickness",comboBoxPcbThickness.Text),            //PCB厚度
                                new XElement("PcbCraft", comboBoxPcbCraft.Text),                   //PCB工艺
                                new XElement("PcbOutDate", dateTimePickerPcbOutDate.Text),         //PCB加工日期
                                new XElement("PcbPath",  textBoxPcbPath.Text),                     //PCB路径
                                new XElement("PcbSoft", PcbSoft.Text),                             //PCB设计软件
                                new XElement("ReserveOne", BomPath.Text),                          //bom料单路径
                                new XElement("ReserveTwo", RecText.Text)                           //PCB设计软件
                        );
                }
                xe.Save(strPath);

                //查找更改已下过单的内容
                findlrder();

            }
            
            getXmlInfo();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //删除XML元素
            if (strID != "")
            {
                //删除PCB文件
                if (File.Exists(textBoxPcbPath.Text))
                {
                    File.Delete(textBoxPcbPath.Text);                       
                }
                //删除料单
                if (File.Exists(RecText.Text))
                {
                    File.Delete(RecText.Text);                       
                }

                //删除料单更改记录
                 if (File.Exists(BomPath.Text))
                {
                    File.Delete(BomPath.Text);                       
                }

                XElement xe = XElement.Load(strPath);
                IEnumerable<XElement> elements = from element in xe.Elements("OrderOut")
                                                 where element.Attribute("ID").Value == strID
                                                 select element;
                if (elements.Count() > 0) 
                {
                    elements.First().Remove();                
                }
                    
                xe.Save(strPath);
            }
            getXmlInfo(); 
        }

        //添加一个配置的行记录
        private void button4_Click(object sender, EventArgs e)
        {
            XElement xe = XElement.Load(strPath);
            IEnumerable<XElement> elements1 = from element in xe.Elements("OrderOut")
                                              select element;
            //生成新的编号
            string str = (Convert.ToInt32(elements1.Max(element => element.Attribute("ID").Value)) + 1).ToString("00000");
            XElement people = new XElement(
                   "OrderOut", new XAttribute("ID", str),
                    new XElement("ProName", textBoxProName.Text),                       //项目名称
                    new XElement("PrintDate", dateTimePickerPrintDate.Text),           //打印日期
                    new XElement("BomFileName", textBoxBomFileName.Text),              //BOM表名
                    new XElement("BomChinaName", textBoxBomChinaName.Text),            //BOM中文名
                    new XElement("PcbName", textBoxPcbName.Text),                      //PCB板名
                    new XElement("PcbRev", comboBoxPcbRev.Text),                       //PCB版本
                    new XElement("PcbSize", textBoxPcbSize.Text),                      //PCB尺寸
                    new XElement("PcbDate", dateTimePickerPcbDate.Text),               //PCB板内日期
                    new XElement("PcbLayer", comboBoxPcbLayer.Text),                   //PCB层数
                    new XElement("PcbNumber", textBoxPcbNumber.Text),                 //PCB下单数量
                    new XElement("PcbThickness", comboBoxPcbThickness.Text),            //PCB厚度
                    new XElement("PcbCraft", comboBoxPcbCraft.Text),                   //PCB工艺
                    new XElement("PcbOutDate", dateTimePickerPcbOutDate.Text),         //PCB加工日期
                    new XElement("PcbPath", textBoxPcbPath.Text),                       //PCB路径
                    new XElement("PcbSoft", PcbSoft.Text),                   //PCB设计软件
                    new XElement("ReserveOne", BomPath.Text),                   //PCB设计软件
                    new XElement("ReserveTwo", RecText.Text)                   //PCB设计软件
                );
            xe.Add(people);
            xe.Save(strPath);
            getXmlInfo();
        }




        //查找
        private void button5_Click(object sender, EventArgs e)
        {
            //dataGridView1.DataSource = null;
             //  .dataSource = null;

            //strID = textBoxFindProName.Text;

            //string xmlFilePath = "Employee.xml";//xml文件存放的路径
            if (textBoxFindProName.Text != "")
            {
                XElement xes = XElement.Load(strPath);//加载xml文件
                //查询指定名称的元素
                var elements = from element in xes.Elements("OrderOut")
                               where element.Element("ProName").Value == textBoxFindProName.Text
                               select new
                               {
                                   项目名称 = element.Element("ProName").Value,
                                   打印日期 = element.Element("PrintDate").Value,
                                   BOM表名 = element.Element("BomFileName").Value,
                                   BOM中文名 = element.Element("BomChinaName").Value,
                                   PCB板名 = element.Element("PcbName").Value,
                                   PCB版本 = element.Element("PcbRev").Value,
                                   PCB尺寸 = element.Element("PcbSize").Value,
                                   PCB板内日期 = element.Element("PcbDate").Value,
                                   PCB层数 = element.Element("PcbLayer").Value,
                                   PCB下单数量 = element.Element("PcbNumber").Value,
                                   PCB厚度 = element.Element("PcbThickness").Value,
                                   PCB工艺 = element.Element("PcbCraft").Value,
                                   PCB加工日期 = element.Element("PcbOutDate").Value,
                                   PCB路径 = element.Element("PcbPath").Value,
                                   软件版本 = element.Element("PcbSoft").Value,   //  new XElement("PcbSoft", "protel99se"),                   //PCB设计软件
                                   BOM路径 = element.Element("ReserveOne").Value,  //new XElement("ReserveOne", "备用1"),                   //PCB设计软件
                                   更改记录路径 = element.Element("ReserveTwo").Value, //new XElement("ReserveTwo", "备用2")                   //PCB设计软件
                                   ID = element.Attribute("ID").Value,// .Element("PcbPath").Value,
                               };

                if (elements.Count() > 0)
                {
                    dataGridView1.DataSource = elements.ToList();
                }
                else
                {
                    MessageBox.Show("没有找到相关的记录。");
                }
                
            }
            else
            {
                getXmlInfo();
            }

           
            

            //XElement xe = XElement.Load(strPath);
            //IEnumerable<XElement> elements = from PInfo in xe.Elements("OrderOut")
            //                                 where PInfo.Attribute("ID").Value == textBoxFindProName.Text
            //                                 select PInfo;
       
            //foreach (XElement element in elements)
            //{
            //    dataGridView1.DataSource = elements;
            //}


            //int i=0;
            //foreach (XElement element in elements)
            //{
            //   //dataGridView1.Rows[i].Cells[0].Value = "测试";

            //    //dataGridView1.Rows[i].Cells[0].Value = element.Element("ProName").Value;
            //    //dataGridView1.Rows[i].Cells[1].Value = element.Element("PrintDate").Value;
            //    //dataGridView1.Rows[i].Cells[2].Value = element.Element("BomFileName").Value;
            //    //dataGridView1.Rows[i].Cells[3].Value = element.Element("BomChinaName").Value;
            //    //dataGridView1.Rows[i].Cells[4].Value = element.Element("PcbName").Value;
            //    //dataGridView1.Rows[i].Cells[5].Value = element.Element("PcbRev").Value;
            //    //dataGridView1.Rows[i].Cells[6].Value = element.Element("PcbSize").Value;
            //    //dataGridView1.Rows[i].Cells[7].Value = element.Element("PcbDate").Value;
            //    //dataGridView1.Rows[i].Cells[8].Value = element.Element("PcbLayer").Value;
            //    //dataGridView1.Rows[i].Cells[9].Value = element.Element("PcbNumber").Value;
            //    //dataGridView1.Rows[i].Cells[10].Value = element.Element("PcbThickness").Value;
            //    //dataGridView1.Rows[i].Cells[11].Value = element.Element("PcbCraft").Value;
            //    //dataGridView1.Rows[i].Cells[12].Value = element.Element("PcbOutDate").Value;
            //    //dataGridView1.Rows[i].Cells[13].Value = element.Element("PcbPath").Value;
            //    i++;
            //}   
        }
        
        //料单文件的目录
        public static string GlobalBomName;
        //料单更改记录的目录
        public static string GlobalBomRecName; 

        //编辑料单
        private void button7_Click(object sender, EventArgs e)
        {
            if (BomPath.Text != "")
            {
                GlobalBomName = BomPath.Text;
                GlobalBomRecName = RecText.Text;
                this.Hide();
                Form2 form2 = new Form2();
                form2.Show();
            }
        }


        //保存PCB文件
        private void button6_Click(object sender, EventArgs e)
        {
            if (SingleBomConfig == true)
            {
                string TempPath = "", SPcbPath = "", DPcbPath = "", TempSuffix = "";
                OpenFileDialog openExcel = new OpenFileDialog();                //实例化打开对话框对象
                openExcel.Filter = "99se文件|*.pcb|AD文件|*.PcbDoc";            //设置打开文件筛选器
                openExcel.Multiselect = false;                                  //设置打开对话框中不能多选

                if (openExcel.ShowDialog() == DialogResult.OK)      //判断是否选择了文件
                {
                    //显示选择的文件目录及文件名 例如C:\BOM\PCB\L1305KZBV10.PcbDoc
                    SPcbPath = openExcel.FileName;
                    TempPath = openExcel.FileName;
                    
                    DateTime TempDateTime = Convert.ToDateTime(GlobalSingleBomConfig[7]);

                    //获取后缀
                    TempSuffix = TempPath.Substring(TempPath.LastIndexOf(".") + 1, TempPath.Length - TempPath.LastIndexOf(".") - 1);
                    DPcbPath = @"C:\BOM\PCB\" + GlobalSingleBomConfig[0] + GlobalSingleBomConfig[2] + GlobalSingleBomConfig[5] + TempDateTime.ToString("yyyy-MM-dd") + "." + TempSuffix;
                    
                    if (MessageBox.Show("是否确定？并保存PCB件到数据库中。", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        int flag = 0;
                        //检查是否存在目的目录
                        if (File.Exists(DPcbPath))
                        {
                            if (MessageBox.Show("数据库中存在此PCB文件，是否替换旧的文件。", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                            {
                                flag = 1;
                            }
                            else
                            {
                                flag = 1;
                            }
                        }
                        else
                        {
                            flag = 1;
                        }

                        if (flag == 1)//不存在就添加进去
                        {
                            GlobalSingleBomConfig[13] = DPcbPath;
                            //从SPcbPath目录复制到时DPcbPath目录中去
                            File.Copy(SPcbPath, DPcbPath, true);
                            SingleBomConfig = false;
                            //添加一条记录
                            XElement xe = XElement.Load(strPath);
                            IEnumerable<XElement> elements = from element in xe.Elements("OrderOut")
                                                             where element.Attribute("ID").Value == strID
                                                             select element;
                            if (elements.Count() > 0)
                            {
                                XElement newXE = elements.First();
                                newXE.SetAttributeValue("ID", strID);
                                newXE.ReplaceNodes(
                                            new XElement("ProName", GlobalSingleBomConfig[0]),                       //项目名称
                                            new XElement("PrintDate", GlobalSingleBomConfig[1]),           //打印日期
                                            new XElement("BomFileName", GlobalSingleBomConfig[2]),              //BOM表名
                                            new XElement("BomChinaName", GlobalSingleBomConfig[3]),            //BOM中文名
                                            new XElement("PcbName", GlobalSingleBomConfig[4]),                      //PCB板名
                                            new XElement("PcbRev", GlobalSingleBomConfig[5]),                       //PCB版本
                                            new XElement("PcbSize", GlobalSingleBomConfig[6]),                      //PCB尺寸
                                            new XElement("PcbDate", GlobalSingleBomConfig[7]),               //PCB板内日期
                                            new XElement("PcbLayer", GlobalSingleBomConfig[8]),                   //PCB层数
                                            new XElement("PcbNumber", GlobalSingleBomConfig[9]),                 //PCB下单数量
                                            new XElement("PcbThickness", GlobalSingleBomConfig[10]),            //PCB厚度
                                            new XElement("PcbCraft", GlobalSingleBomConfig[11]),                   //PCB工艺
                                            new XElement("PcbOutDate", GlobalSingleBomConfig[12]),         //PCB加工日期
                                            new XElement("PcbPath", GlobalSingleBomConfig[13]),                      //PCB路径
                                            new XElement("PcbSoft", GlobalSingleBomConfig[14]),                              //PCB设计软件
                                            new XElement("ReserveOne", GlobalSingleBomConfig[15]),                           //bom料单路径
                                            new XElement("ReserveTwo", GlobalSingleBomConfig[16])                            //PCB设计软件
                                    );
                                xe.Save(strPath);
                                //重新加载一下     
                                getXmlInfo();
                            }
                        }//添加记录结束                                         
                    }//提示是否保存到数据库中              
                }
                else
                {
                    //   MessageBox("没有选择文件");
                }
            }
        }

        int countselectbom = 0;
        string[] selectbomstr = new string[100];
        //
        string[] selectbomname = new string[100];
        
        //存储要输出单表的配置文件
        List<string> OutAllBomList = new List<string>();

        private void button8_Click(object sender, EventArgs e)
        {
            if (strID != "")
	        {
                this.Width = 1280;
                this.Height = 615;
                this.FormBorderStyle = FormBorderStyle.FixedDialog;
                button10.Text = "<-";

                int tflag = 0;
                if (countselectbom == 0)
                {
                    tflag = 0; //要添加
                }
                else
                {
                    tflag = 0; //要添加
                    for (int i = 0; i < countselectbom; i++)
                    {
                        if (selectbomstr[i] == textBoxBomChinaName.Text)
                        {
                            MessageBox.Show("已经添加过，请不要重复添加。");
                            tflag = 1;
                            break;
                        }
                        else
                        {
                            if (selectbomname[i] == textBoxBomFileName.Text) 
                            {
                                MessageBox.Show("bom表名重复，请不要重复添加。");
                                tflag = 1;
                                break;
                            }

                        }
                    }

                }

                if (tflag == 0)
                {
                    checkedListBox1.Items.Add(textBoxBomChinaName.Text);
                    checkedListBox1.SetItemChecked(countselectbom, true);
                    selectbomstr[countselectbom] = textBoxBomChinaName.Text;
                    selectbomname[countselectbom] = textBoxBomFileName.Text;

                    OutAllBomList.Add(textBoxProName.Text);                 //项目名称
                    OutAllBomList.Add(dateTimePickerPrintDate.Text);        //打印日期
                    OutAllBomList.Add(textBoxBomFileName.Text);             //BOM表名
                    OutAllBomList.Add(textBoxBomChinaName.Text);            //BOM中文名
                    OutAllBomList.Add(textBoxPcbName.Text);                 //PCB板名
                    OutAllBomList.Add(comboBoxPcbRev.Text);                 //PCB版本
                    OutAllBomList.Add(textBoxPcbSize.Text);                 //PCB尺寸
                    OutAllBomList.Add(dateTimePickerPcbDate.Text);          //PCB板内日期
                    OutAllBomList.Add(comboBoxPcbLayer.Text);               //层数
                    OutAllBomList.Add(textBoxPcbNumber.Text);               //PCB下单数量
                    OutAllBomList.Add(comboBoxPcbThickness.Text);           //PCB厚度
                    OutAllBomList.Add(comboBoxPcbCraft.Text);               //PCB工艺
                    OutAllBomList.Add(dateTimePickerPcbOutDate.Text);       //PCB加工日期
                    OutAllBomList.Add(textBoxPcbPath.Text);                 //PCB路径
                    OutAllBomList.Add(PcbSoft.Text);                        //PCB设计软件
                    OutAllBomList.Add(BomPath.Text);                        //料单路径
                    OutAllBomList.Add(RecText.Text);                        //更改记录路径  
                    countselectbom++;
                }
	        }            
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (button10.Text=="->")
            {
                button10.Text = "<-";
                this.Width = 1280;
                this.Height = 615;
                this.FormBorderStyle = FormBorderStyle.FixedDialog;                
            }
            else
            {
                button10.Text = "->";
                this.Width = 980;
                this.Height = 615;
                this.FormBorderStyle = FormBorderStyle.FixedDialog;
            }
        }


        //检查输入是否完整
        public bool checkinput()
        {
            if ((AllTitle.Text != "(样板)L1305机车信号") && (textBoxAllRev.Text != "(样板)V10") && (textBoxcompile.Text != "(样板)何政") && (textBoxCheck.Text != "(样板)林世春") && (textBoxRatify.Text != "(样板)牛意坚") && (textBox7.Text != "(样板)机车信号，生产300，"))
            {
                if (countselectbom > 0)
                {
                    if (countselectbom == 1)//说明只输出一个表
                    {
                        MessageBox.Show("输出的单个的表，请在编辑料单中输出。", "提示");
                        return false;
                    }
                    else
                    {
                        return true; 
                    }
                                       
                }
                else
                {
                    MessageBox.Show("你还没有添加要输出的表。", "提示");
                    return false;
                }
                
                
            }
            else
            {
                MessageBox.Show("总表配置，请输入完整。","提示");
                return false;
            }           
        }

        public void addconfig(int i)
        {
            //string TempPath = @"C:\BOM\OUT\OutOrderOutTitle.xml";

            XElement xe = XElement.Load(@"C:\BOM\OUT\OutOrderOutS.xml");
            IEnumerable<XElement> elements1 = from element in xe.Elements("OrderOut")
                                              select element;
            //生成新的编号
            string str = (Convert.ToInt32(elements1.Max(element => element.Attribute("ID").Value)) + 1).ToString("00000");

            XElement people = new XElement(
                   "OrderOut", new XAttribute("ID", str),
                    new XElement("OutID", GlobalStrPath),                                   //定单序号
                    new XElement("ProName", OutAllBomList[i+0]),                       //项目名称
                    new XElement("PrintDate", OutAllBomList[i + 1]),                 //打印日期
                    new XElement("BomFileName", OutAllBomList[i + 2]),               //BOM表名
                    new XElement("BomChinaName", OutAllBomList[i + 3]),              //BOM中文名
                    new XElement("PcbName", OutAllBomList[i + 4]),                   //PCB板名
                    new XElement("PcbRev", OutAllBomList[i + 5]),                    //PCB版本
                    new XElement("PcbSize", OutAllBomList[i + 6]),                   //PCB尺寸
                    new XElement("PcbDate", OutAllBomList[i + 7]),                   //PCB板内日期
                    new XElement("PcbLayer", OutAllBomList[i + 8]),                  //PCB层数
                    new XElement("PcbNumber", OutAllBomList[i + 9]),                 //PCB下单数量
                    new XElement("PcbThickness", OutAllBomList[i + 10]),             //PCB厚度
                    new XElement("PcbCraft", OutAllBomList[i + 11]),                 //PCB工艺
                    new XElement("PcbOutDate", OutAllBomList[i + 12]),               //PCB加工日期
                    new XElement("PcbPath", OutAllBomList[i + 13]),                  //PCB路径
                    new XElement("PcbSoft", OutAllBomList[i + 14]),                  //PCB设计软件
                    new XElement("ReserveOne", OutAllBomList[i + 15]),               //料单路径
                    new XElement("ReserveTwo", OutAllBomList[i + 16])                //更改记录路径
                );
            xe.Add(people);
            xe.Save(@"C:\BOM\OUT\OutOrderOutS.xml");
            //getXmlInfo();

            //0 OutAllBomList.Add(textBoxProName.Text);                 //项目名称
            //1 OutAllBomList.Add(dateTimePickerPrintDate.Text);        //打印日期
            //2 OutAllBomList.Add(textBoxBomFileName.Text);             //BOM表名
            //3 OutAllBomList.Add(textBoxBomChinaName.Text);            //BOM中文名
            //4 OutAllBomList.Add(textBoxPcbName.Text);                 //PCB板名
            //5 OutAllBomList.Add(comboBoxPcbRev.Text);                 //PCB版本
            //6 OutAllBomList.Add(textBoxPcbSize.Text);                 //PCB尺寸
            //7 OutAllBomList.Add(dateTimePickerPcbDate.Text);          //PCB板内日期
            //8 OutAllBomList.Add(comboBoxPcbLayer.Text);               //层数
            //9 OutAllBomList.Add(textBoxPcbNumber.Text);               //PCB下单数量
            //10 OutAllBomList.Add(comboBoxPcbThickness.Text);           //PCB厚度
            //11 OutAllBomList.Add(comboBoxPcbCraft.Text);               //PCB工艺
            //12 OutAllBomList.Add(dateTimePickerPcbOutDate.Text);       //PCB加工日期
            //13 OutAllBomList.Add(textBoxPcbPath.Text);                 //PCB路径
            //14 OutAllBomList.Add(PcbSoft.Text);                        //PCB设计软件
            //15 OutAllBomList.Add(BomPath.Text);                        //料单路径
            //16 OutAllBomList.Add(RecText.Text);                        //更改记录路径 

        }


        //添加一个总表标题记录
        static string GlobalStrPath="";  

        public void addallconfig()
        {
           string TempPath = @"C:\BOM\OUT\OutOrderOutTitle.xml";

           XElement xe = XElement.Load(TempPath);
           IEnumerable<XElement> elements1 = from element in xe.Elements("OrderOut")
                                             select element;
           //生成新的编号
           GlobalStrPath = (Convert.ToInt32(elements1.Max(element => element.Attribute("ID").Value)) + 1).ToString("00000");

           XElement people = new XElement(
                   "OrderOut", new XAttribute("ID", GlobalStrPath),
                     new XElement("ProTitle", AllTitle.Text),//"(样板)L1305机车信号"),         //总表标题
                     new XElement("ProRev", textBoxAllRev.Text),//"(样板)V10"),                 //总表版本
                     new XElement("PrintDate",Convert.ToDateTime(dateTimePicker1.Text).ToString("yyyy-MM-dd")),  // dateTimePicker1.Text) 打印日期
                     new XElement("ProCompile", textBoxcompile.Text),//"(样板)何政"),             //编制
                     new XElement("ProCheck", textBoxCheck.Text),//"(样板)林世春"),                //审核
                     new XElement("ProRatify", textBoxRatify.Text),//"(样板)牛意坚"),                 //批准
                     new XElement("ProNote",textBox7.Text)// "(样板)机车信号，生产300，")    //输出说明
                );
            xe.Add(people);
            xe.Save(TempPath);            
           // return str;                 

        }


        public void sss()
        {      
            //DataSet myds = new DataSet();
            //myds.ReadXml(strPath);
            //if (myds.Tables.Count > 0)
            //{
            //    dataGridView1.DataSource = myds.Tables[0];

               


            //    int sheetnum = 0;
            //    for (int j = 1; j <= OutWorkBook.Worksheets.Count; j++)
            //    {
            //        if (OutWorkBook.Worksheets[j].Name == "料单样板")
            //        {
            //            sheetnum = j;
            //        }
            //    }

            //    SingleBomSheet = OutWorkBook.Worksheets[sheetnum];
            //    //写入到要输出的单表

            //    for (int rows = 0; rows < dataGridView1.Rows.Count - 1; rows++)
            //    {
            //        SingleBomSheet.Cells[rows + 3, 1] = dataGridView1.Rows[rows].Cells[0].Value.ToString();//序号
            //        //SingleBomSheet.Cells[rows + 3, 2] = dataGridView1.Rows[rows].Cells[1].Value.ToString();//统一编号
            //        SingleBomSheet.Cells[rows + 3, 3] = dataGridView1.Rows[rows].Cells[2].Value.ToString();//器件名称
            //        SingleBomSheet.Cells[rows + 3, 4] = dataGridView1.Rows[rows].Cells[3].Value.ToString();//器件型号
            //        SingleBomSheet.Cells[rows + 3, 5] = dataGridView1.Rows[rows].Cells[4].Value.ToString();//器件封装
            //        SingleBomSheet.Cells[rows + 3, 6] = dataGridView1.Rows[rows].Cells[5].Value.ToString();//器件数量
            //        SingleBomSheet.Cells[rows + 3, 7] = dataGridView1.Rows[rows].Cells[6].Value.ToString();//器件编号
            //        //SingleBomSheet.Cells[rows + 3, 8] = dataGridView1.Rows[rows].Cells[7].Value.ToString();//加工数量
            //        SingleBomSheet.Cells[rows + 3, 9] = dataGridView1.Rows[rows].Cells[8].Value.ToString();//备注
            //    }
            //    string TempPath = Form1.GlobalBomName, Tempstr;
            //    //BOM文件名
            //    Tempstr = TempPath.Substring(TempPath.LastIndexOf("\\") + 1, TempPath.LastIndexOf(".") - (TempPath.LastIndexOf("\\") + 1)); //获取文件名
            //    //获取文件扩展名
            //    //TempPath.Substring(TempPath.LastIndexOf(".") + 1, TempPath.Length - TempPath.LastIndexOf(".") - 1)

            //    //自定义页眉
            //    SingleBomSheet.PageSetup.LeftHeader = @"&""微软雅黑,常规""文件编号: QR/JS-7.3-13";
            //    //输出项目名称+BOM名+版本+中文名
            //    SingleBomSheet.PageSetup.CenterHeader = @"&""微软雅黑,常规""&18" + Tempstr + "元件清单";
            //    SingleBomSheet.PageSetup.RightHeader = @"&""微软雅黑,常规""版本: V10";// textBoxBomRev.Text;
            //    //自定义页脚
            //    SingleBomSheet.PageSetup.LeftFooter = @"&""微软雅黑,常规""日期:" + DateTime.Now.ToString("yyyy-MM-dd");
            //    SingleBomSheet.PageSetup.CenterFooter = @"&""微软雅黑,常规""第&P页/共&N页";
            //    SingleBomSheet.PageSetup.RightFooter = "";

            //    OutWorkBook.Save();//保存工作表
            //    OutWorkBook.Close(false, missing, missing);//关闭工作表

            //    //MessageBox.Show("导出完成。");
            //}//判断是否有记录，有记录才能存入       
        }


        string[] BomFileName = new string[100];                  //存放打个BOM时的文件名
        int BomNumCount = 0;                                    //存打开多个BOM时的个数
        //存储总表所有元件的未整理时的内容（即合并完成后自动存入到这里）
        List<string> OkAllList = new List<string>();

        //存储所有排序表内容
        List<string> GAllSortList = new List<string>();
        bool LibFlag = true;

        public void menfun()
        {
            string AllLibPath = @"C:\BOM\排序表.txt", TempStr; 
            if (LibFlag)
            {
                //读取退出
                StreamReader ReplaceReader = new StreamReader(AllLibPath, Encoding.Default);  //实例化流读取对象
                while ((TempStr = ReplaceReader.ReadLine()) != null)                                           //循环读取文本文件中的每一行
                {
                    GAllSortList.Add(TempStr.ToUpper());                                                                 //将读取到的行内容添加到泛型集合中
                }
                ReplaceReader.Close();
                ReplaceReader.Dispose();             
                LibFlag = false; //读取完成标志已读
            }//读取结束       
         }


        //汇总表输出
        public void CollectOut(string filename)
        {
            toolStripStatusLabel1.Text = "正在生成总表，请稍后。";
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//实例化Excel对象
            object missing = System.Reflection.Missing.Value;//获取缺少的object类型值
            //打开指定的Excel文件
            Microsoft.Office.Interop.Excel.Workbook workbook = excel.Application.Workbooks.Open(@filename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            workbook.Application.DisplayAlerts = false;

            int AllBom = 0;
            for (int i = 1; i <= workbook.Worksheets.Count; i++)
            {
                if (workbook.Worksheets[i].Name == "总表样板")
                {
                    AllBom = i;
                }
            }

            Excel.Worksheet AllBomSheet = workbook.Worksheets[AllBom];//要操作的表的索引
            AllBomSheet.Name = "总表";
            Excel.Range range;

            int TempExcelRow = 5, IDnum = 1; //第5行开始

            //存内容到全局变量上
           // Addlist();            

            //更改列宽，重新分配打开多个表的列宽
            //StartColumn 开始列为5，
            //EndColumn = 结束列为5  + BomNumCount + 6 
            //TabNum = 表的结束列 BomNumCount + 6;
            int DelTab, addcol;// = 22 - BomNumCount; 
            int StartColumn = 5, EndColumn = BomNumCount + 6 + 5, TabNum = BomNumCount + 6;
            double TabWidth = (119 / (BomNumCount + 6)) + 0.6;
            if (TabWidth < 1)
            {
                TabWidth = 1;
            }

            if (BomNumCount < 23)
            {
                DelTab = 22 - BomNumCount;
                while (DelTab != 0)
                {
                    range = AllBomSheet.Columns["G:G", System.Type.Missing];
                    range.Columns.Delete();
                    DelTab--;
                }
            }
            else
            {
                addcol = BomNumCount - 22;
                while (addcol != 0)
                {
                    range = AllBomSheet.Columns["G:G", System.Type.Missing];
                    range.Insert(Excel.XlDirection.xlDown, missing);
                    addcol--;
                }
            }

            while (StartColumn != EndColumn)
            {
                range = AllBomSheet.Columns[GetColumnName(StartColumn) + ":" + GetColumnName(StartColumn), System.Type.Missing];
                range.ColumnWidth = TabWidth;
                StartColumn++;
            }


              int[] tempcountid = new int[100];
            int count00=0;
              for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    if (checkedListBox1.GetItemChecked(i) == true)
                    {
                        tempcountid[i] = i ;
                        count00++;
                    }
                }

            //表名的表头
            for (int i = 0; i < BomNumCount; i++)
            {
                AllBomSheet.Cells[1, 7 + i] = BomFileName[i];

                AllBomSheet.Cells[4, 7 + i] = BomFileName[i];

                AllBomSheet.Cells[2, 7 + i] =  OutAllBomList[( tempcountid[i] * 17 )+ 9];
                AllBomSheet.Cells[3, 7 + i] =  OutAllBomList[( tempcountid[i] * 17 )+ 9];
            }

            //写入所有行
            int count = (OkAllList.Count / 4) + 5;
            for (int i = 5; i < count; i++)
            {
                AllBomSheet.Cells[TempExcelRow, 1] = OkAllList[((i - 5) * 4) + 0];
                AllBomSheet.Cells[TempExcelRow, 2] = OkAllList[((i - 5) * 4) + 1];
                AllBomSheet.Cells[TempExcelRow, 3] = OkAllList[((i - 5) * 4) + 2];
                AllBomSheet.Cells[TempExcelRow, 4] = OkAllList[((i - 5) * 4) + 3];

                string K1 = "", K2 = "=", K4 = "=";
                //表名的表头对应的值
                for (int f = 0; f < BomNumCount; f++)
                {
                    AllBomSheet.Cells[TempExcelRow, 7 + f] = @"=SUMIFS(" + BomFileName[f] + "!F:F," + BomFileName[f] + "!D:D,总表!C" + TempExcelRow + "," + BomFileName[f] + "!E:E,总表!D" + TempExcelRow + ")";
                }

                K1 = ""; K2 = "="; K4 = "=";
                for (int k = 7; k < 7 + BomNumCount; k++)
                {
                    K1 = GetColumnName(k);
                    if (k == (7 + BomNumCount - 1))
                    {
                        K2 = K2 + K1 + "$2*" + K1 + TempExcelRow;
                        K4 = K4 + K1 + "$3*" + K1 + TempExcelRow;
                    }
                    else
                    {
                        K2 = K2 + K1 + "$2*" + K1 + TempExcelRow + "+";
                        K4 = K4 + K1 + "$3*" + K1 + TempExcelRow + "+";
                    }
                }
                AllBomSheet.Cells[TempExcelRow, 7 + BomNumCount] = @K2;
                AllBomSheet.Cells[TempExcelRow, 7 + BomNumCount + 1] = @K4;
                TempExcelRow++; IDnum++;
            }

            //进行排一下序AllSheet.Cells[i, 10 + BomNumCount]
            range = AllBomSheet.Range[AllBomSheet.Cells[5, 1], AllBomSheet.Cells[count-1,4]];//10 + BomNumCount + 1]
            //按3列升序排序
            range.Sort(
             range.Columns[2, missing], Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
             range.Columns[4, missing], missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
             range.Columns[3, missing], Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
             Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, missing, missing,
             Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns,
             Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin,
             Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
             Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
             Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal);

            menfun();
            //--------------------------sortfun();-------------------------------------
            //暂存总表内容
            List<string> TempList = new List<string>();
            string stemp; //int id00 = 1;
            for (int p = 5; p < 200000; p++)
            {
                range = AllBomSheet.Range[AllBomSheet.Cells[p, 1], AllBomSheet.Cells[p, 1]];
                stemp = range.Text.ToString();
                if (stemp != "")
                {
                    //TempList.Add(id00.ToString()); id00++;
                    TempList.Add(stemp);
                    range = AllBomSheet.Range[AllBomSheet.Cells[p, 2], AllBomSheet.Cells[p, 2]];
                    stemp = range.Text.ToString(); TempList.Add(stemp);
                    range = AllBomSheet.Range[AllBomSheet.Cells[p, 3], AllBomSheet.Cells[p, 3]];
                    stemp = range.Text.ToString(); TempList.Add(stemp);
                    range = AllBomSheet.Range[AllBomSheet.Cells[p, 4], AllBomSheet.Cells[p, 4]];
                    stemp = range.Text.ToString(); TempList.Add(stemp);
                }
                else
                {
                    break;
                }
            }


            //开始排序
            List<string> TempList2 = new List<string>();
            //string[] tempint = new string[TempList.Count / 4];
            //int tempints = 0;
            for (int ai = 0; ai < GAllSortList.Count; )
            {
                for (int jj = 0; jj < TempList.Count; )
                {
                    if (GAllSortList[ai] == TempList[jj + 1])
                    {
                        //tempint[tempints] = TempList[jj + 0]; 
                        //tempints++;
                        TempList2.Add(TempList[jj + 0]);
                        TempList2.Add(GAllSortList[ai + 1]);TempList[jj + 1] = GAllSortList[ai + 1];
                        TempList2.Add(TempList[jj + 2]);
                        TempList2.Add(TempList[jj + 3]);
                    }
                    jj = jj + 4;
                }
                ai = ai + 2; ;
            }

            //重新填入
            int ttmp = 0, lentht = TempList2.Count / 4 + 5;
            for (int lls = 5; lls < lentht; lls++)
            {
                AllBomSheet.Cells[lls, 1] = TempList2[ttmp * 4 + 0];
                AllBomSheet.Cells[lls, 2] = TempList2[ttmp * 4 + 1];
                AllBomSheet.Cells[lls, 3] = TempList2[ttmp * 4 + 2];
                AllBomSheet.Cells[lls, 4] = TempList2[ttmp * 4 + 3];
                ttmp++;
            }

            int ttid = lentht, idd2f = 0;
            for (int inbom = 0; inbom < TempList.Count; )
            {
                for (int d = 0; d < TempList2.Count; )
                {
                    if (TempList[inbom + 1] == TempList2[d + 1])
                    {
                        idd2f = 1;
                        break;
                    }
                    d = d + 4;
                }
                if (idd2f != 1)
                {
                    idd2f = 0;
                    AllBomSheet.Cells[ttid, 1] = TempList[inbom + 0];
                    AllBomSheet.Cells[ttid, 2] = TempList[inbom + 1];
                    AllBomSheet.Cells[ttid, 3] = TempList[inbom + 2];
                    AllBomSheet.Cells[ttid, 4] = TempList[inbom + 3];
                    ttid++;
                }
                else
                {
                    idd2f = 0;
                }
                inbom = inbom + 4;
            }
            //--------------------------sortfun();-------------------------------------




            //重新生成一个新排列序号
            int ids = 1;
            for (int k = 5; k < 20000; k++)
            {
                range = AllBomSheet.Range[AllBomSheet.Cells[k, 1], AllBomSheet.Cells[k, 1]];
                if (range.Text.ToString() != "")
                {
                    AllBomSheet.Cells[k, 1] = Convert.ToString(ids); ids++;
                }
                else
                {
                    break;
                }
            } 

            //自定义页眉
            AllBomSheet.PageSetup.LeftHeader = @"&""微软雅黑,常规""文件编号: QR/JS-7.3-13";
            AllBomSheet.PageSetup.CenterHeader = @"&""微软雅黑,常规""&18" + AllTitle.Text + "汇总表";
            AllBomSheet.PageSetup.RightHeader = @"&""微软雅黑,常规""序号:" + GlobalStrPath;// textBoxAllRev.Text textBoxBomRev.Text;
            //自定义页脚
            AllBomSheet.PageSetup.LeftFooter = @"▲代表关键器件";
            AllBomSheet.PageSetup.CenterFooter = @"&""微软雅黑,常规""第&P页/共&N页";
            AllBomSheet.PageSetup.RightFooter = "";


            //给分表加索引
            int excelrow = 3;
            string TempStr1 = "123";
            Excel.Worksheet SingleSheet;
            for (int s = 0; s < BomNumCount; s++)
            {
                //AllBomSheet.Cells[1, 7 + s] = BomFileName[s];
                //AllBomSheet.Cells[4, 7 + s] = BomFileName[s];

                for (int n = 1; n <= workbook.Worksheets.Count; n++)
                {
                    if (workbook.Worksheets[n].Name == BomFileName[s])
                    {
                        SingleSheet = workbook.Worksheets[n];//要操作的表的索引。
                        SingleSheet.Cells[2, 8] = "=总表!" + GetColumnName(7 + s) + "3";
                
                        //AllBomSheet.Cells[2, 7 + i] =  OutAllBomList[( tempcountid[i] * 17 )+ 9];
                        //AllBomSheet.Cells[3, 7 + i] =  OutAllBomList[( tempcountid[i] * 17 )+ 9];

                        while (true)
                        {
                            range = SingleSheet.Range[SingleSheet.Cells[excelrow, 1], SingleSheet.Cells[excelrow, 1]];
                            TempStr1 = range.Text.ToString();
                            if (TempStr1 != "")
                            {
                                SingleSheet.Cells[excelrow, 10] = @"=SUMIFS(总表!" + GetColumnName(7 + s) + ":" + GetColumnName(7 + s) + ",总表!C:C," + SingleSheet.Name + "!D" + excelrow + ",总表!D:D," + SingleSheet.Name + "!E" + excelrow;
                                SingleSheet.Cells[excelrow, 8] = "=H$2*F" + excelrow;
                                excelrow++;
                            }
                            else
                            {
                                excelrow = 3;
                                break;
                            }
                        }
                    }
                }
            }

            workbook.Save();//保存工作表
            workbook.Close(false, missing, missing);//关闭工作表

        }

        //输出料单
        private void button9_Click(object sender, EventArgs e)
        {
            int[] tempcountid = new int[100];
            int count = 0;

            string partnum,partname, parttype, partfoot;
            int tempexist=0;

            if (checkinput())
            {
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    if (checkedListBox1.GetItemChecked(i) == true)
                    {
                        tempcountid[i] = i ;
                        count++;
                    }
                }

                if (count > 1)
                {
                    
                    SaveFileDialog saveexcel = new SaveFileDialog();
                    saveexcel.Filter = "Excel文件|*.xlsx";

                    if (saveexcel.ShowDialog() == DialogResult.OK)
                    {
                        BomNumCount = count; //输出要加入的表数

                        File.Copy(@"C:\BOM\Excel2007样板.xlsx", @saveexcel.FileName, true);

                        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//实例化Excel对象
                        object missing = System.Reflection.Missing.Value;//获取缺少的object类型值
                        //打开指定的Excel文件
                        Microsoft.Office.Interop.Excel.Workbook OutWorkBook = excel.Application.Workbooks.Open(@saveexcel.FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                        OutWorkBook.Application.DisplayAlerts = false;
                        Excel.Worksheet SingleBomSheet;
                        Excel.Range range;
                        //复制一个表
                        object Missing = System.Reflection.Missing.Value;                   //获取缺少的object类型值                     

                        //添加一个总表标题配置
                        addallconfig();
                        // int j = 0;
                        
                        OkAllList.Clear();                    
                        

                        for (int i = 0; i < count; i++)
                        {

                            int ci = tempcountid[i] * 17;

                            //存入表名内容
                            BomFileName[i] = OutAllBomList[ci + 2];
                            toolStripStatusLabel1.Text = "正在生成单表，" + OutAllBomList[ci + 2]  + "请稍后。";
                            //添加一个记录
                            addconfig(ci);
                            //生成单表
                            //生成单表函数

                            DataSet myds = new DataSet();
                            myds.ReadXml(OutAllBomList[ci + 15]);//加载料单路径
                            if (myds.Tables.Count > 0)
                            {
                                dataGridView1.DataSource = myds.Tables[0];

                                int sheetnum = 0;
                                for (int j = 1; j <= OutWorkBook.Worksheets.Count; j++)
                                {
                                    if (OutWorkBook.Worksheets[j].Name == "料单样板")
                                    {
                                        sheetnum = j;
                                    }
                                }
                                SingleBomSheet = OutWorkBook.Worksheets[sheetnum];
                                //写入到要输出的单表
                                if ( i+1 < count)
                                {   //复制一个单表
                                    SingleBomSheet.Copy(Missing, SingleBomSheet);
                                    //要操作的表的索引。 
                                    SingleBomSheet = OutWorkBook.Worksheets[sheetnum + 1];
                                }
                   
                                SingleBomSheet.Name = OutAllBomList[ci + 2];//表名

                                for (int rows = 0; rows < dataGridView1.Rows.Count - 1; rows++)
                                {   
                                    partnum = dataGridView1.Rows[rows].Cells[0].Value.ToString();
                                    partname = dataGridView1.Rows[rows].Cells[2].Value.ToString();
                                    parttype = dataGridView1.Rows[rows].Cells[3].Value.ToString();
                                    partfoot = dataGridView1.Rows[rows].Cells[4].Value.ToString();

                                    if (partnum != "nc删除")
                                    { SingleBomSheet.Cells[rows + 3, 1] = partnum; }  //序号
                                    //SingleBomSheet.Cells[rows + 3, 2] = dataGridView1.Rows[rows].Cells[1].Value.ToString();//统一编号
                                    if (partname != "nc删除")
                                    { SingleBomSheet.Cells[rows + 3, 3] = partname;}//器件名称                                   
                                    if (parttype != "nc删除")
                                    { SingleBomSheet.Cells[rows + 3, 4] = parttype;}//器件型号                                    
                                    if (partfoot != "nc删除")
                                    {SingleBomSheet.Cells[rows + 3, 5] = partfoot;}//器件封装
                                    if (dataGridView1.Rows[rows].Cells[5].Value.ToString() != "nc删除")
                                    {SingleBomSheet.Cells[rows + 3, 6] = dataGridView1.Rows[rows].Cells[5].Value.ToString();}//器件数量
                                     if (dataGridView1.Rows[rows].Cells[6].Value.ToString() != "nc删除")
                                     {SingleBomSheet.Cells[rows + 3, 7] = dataGridView1.Rows[rows].Cells[6].Value.ToString();}//器件编号
                                     //SingleBomSheet.Cells[rows + 3, 8] = dataGridView1.Rows[rows].Cells[7].Value.ToString();//加工数量
                                     if (dataGridView1.Rows[rows].Cells[8].Value.ToString() != "nc删除")
                                     {SingleBomSheet.Cells[rows + 3, 9] = dataGridView1.Rows[rows].Cells[8].Value.ToString();}//备注   

                                    //查重复并存入
                                    if (OkAllList.Count > 3)
                                    {
                                        for (int k  = 0; k < OkAllList.Count; )
                                        {
                                            if ((OkAllList[k+1] == partname) && (OkAllList[k+2] == parttype) && (OkAllList[k+3] == partfoot))
                                            {
                                                tempexist = 10;
                                                break; 
                                            }
                                            k = k + 4;
                                        }

                                        if (tempexist != 10)
                                        {
                                            OkAllList.Add(partnum);//序号
                                            OkAllList.Add(partname);//器件名称
                                            OkAllList.Add(parttype);//器件型号
                                            OkAllList.Add(partfoot);//器件封装   
                                            tempexist = 0;
                                        }
                                        tempexist = 0;
                                    }
                                    else
                                    {
                                        OkAllList.Add(partnum);//序号
                                        OkAllList.Add(partname);//器件名称
                                        OkAllList.Add(parttype);//器件型号
                                        OkAllList.Add(partfoot);//器件封装                                    
                                    }
                                }

                                int rowstemp = dataGridView1.Rows.Count - 2;
                                //进行排一下序
                                range = SingleBomSheet.Range[SingleBomSheet.Cells[3, 1], SingleBomSheet.Cells[rowstemp, 9]];
                                range.Sort(
                                             range.Columns[3, missing], Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                                             range.Columns[5, missing], missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                                             range.Columns[4, missing], Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                                             Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, missing, missing,
                                             Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns,
                                             Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin,
                                             Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                                             Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                                             Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal);
                                menfun();


                                //暂存分表内容
                                List<string> TempList = new List<string>();
                                string stemp;
                                for (int p = 3; p < 200000; p++)
                                {
                                    range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 1], SingleBomSheet.Cells[p, 1]];
                                    stemp = range.Text.ToString();
                                    if (stemp != "")
                                    {
                                        TempList.Add(stemp);
                                        range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 3], SingleBomSheet.Cells[p, 3]];
                                        stemp = range.Text.ToString(); TempList.Add(stemp);
                                        range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 4], SingleBomSheet.Cells[p, 4]];
                                        stemp = range.Text.ToString(); TempList.Add(stemp);
                                        range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 5], SingleBomSheet.Cells[p, 5]];
                                        stemp = range.Text.ToString(); TempList.Add(stemp);
                                        range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 6], SingleBomSheet.Cells[p, 6]];
                                        stemp = range.Text.ToString(); TempList.Add(stemp);
                                        range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 7], SingleBomSheet.Cells[p, 7]];
                                        stemp = range.Text.ToString(); TempList.Add(stemp);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                //开始排序
                                List<string> TempList2 = new List<string>();
                                for (int ai = 0; ai < GAllSortList.Count; )
                                {
                                    for (int jj = 0; jj < TempList.Count; )
                                    {
                                        if (GAllSortList[ai] == TempList[jj + 1])
                                        {
                                            TempList2.Add(TempList[jj + 0]);
                                            TempList2.Add(GAllSortList[ai + 1]);TempList[jj + 1] = GAllSortList[ai + 1];//TempList[jj + 1]
                                            TempList2.Add(TempList[jj + 2]);
                                            TempList2.Add(TempList[jj + 3]);
                                            TempList2.Add(TempList[jj + 4]);
                                            TempList2.Add(TempList[jj + 5]);
                                        }
                                        jj = jj + 6;
                                    }
                                    ai = ai + 2; ;
                                }

                                //重新填入
                                int ttmp = 0, lentht = TempList2.Count / 6 + 3;
                                for (int lls = 3; lls < lentht; lls++)
                                {
                                    SingleBomSheet.Cells[lls, 1] = TempList2[ttmp * 6 + 0];
                                    SingleBomSheet.Cells[lls, 3] = TempList2[ttmp * 6 + 1];
                                    SingleBomSheet.Cells[lls, 4] = TempList2[ttmp * 6 + 2];
                                    SingleBomSheet.Cells[lls, 5] = TempList2[ttmp * 6 + 3];
                                    SingleBomSheet.Cells[lls, 6] = TempList2[ttmp * 6 + 4];
                                    SingleBomSheet.Cells[lls, 7] = TempList2[ttmp * 6 + 5];
                                    ttmp++;
                                }

                                int ttid = lentht, idd2f = 0;
                                for (int inbom = 0; inbom < TempList.Count; )
                                {
                                    for (int d = 0; d < TempList2.Count; )
                                    {
                                        if (TempList[inbom + 1] == TempList2[d + 1])
                                        {
                                            idd2f = 1;
                                            break;
                                        }
                                        d = d + 6;
                                    }
                                    if (idd2f != 1)
                                    {
                                        idd2f = 0;
                                        SingleBomSheet.Cells[ttid, 1] = TempList[inbom + 0];
                                        SingleBomSheet.Cells[ttid, 3] = TempList[inbom + 1];
                                        SingleBomSheet.Cells[ttid, 4] = TempList[inbom + 2];
                                        SingleBomSheet.Cells[ttid, 5] = TempList[inbom + 3];
                                        SingleBomSheet.Cells[ttid, 6] = TempList[inbom + 4];
                                        SingleBomSheet.Cells[ttid, 7] = TempList[inbom + 5];
                                        ttid++;
                                    }
                                    else
                                    {
                                        idd2f = 0;
                                    }
                                    inbom = inbom + 6;
                                }
                                


                                //重新生成一个新排列序号
                                int ids = 1;
                                for (int k = 3; k < 20000; k++)
                                {
                                    range = SingleBomSheet.Range[SingleBomSheet.Cells[k, 1], SingleBomSheet.Cells[k, 1]];
                                    if (range.Text.ToString() != "")
                                    {
                                        SingleBomSheet.Cells[k, 1] = Convert.ToString(ids); ids++;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                } 

                                //自定义页眉
                                SingleBomSheet.PageSetup.LeftHeader = @"&""微软雅黑,常规""文件编号: QR/JS-7.3-13";
                                //输出项目名称+BOM名+版本+中文名
                                SingleBomSheet.PageSetup.CenterHeader = @"&""微软雅黑,常规""&18" + OutAllBomList[ci + 0] + OutAllBomList[ci + 2] + OutAllBomList[ci + 5] + "(" + OutAllBomList[ci + 3] + ")元件清单";
                                SingleBomSheet.PageSetup.RightHeader = @"&""微软雅黑,常规""版本: V10";// textBoxBomRev.Text;
                                //自定义页脚
                                SingleBomSheet.PageSetup.LeftFooter = @"&""微软雅黑,常规""日期:" + DateTime.Now.ToString("yyyy-MM-dd");
                                SingleBomSheet.PageSetup.CenterFooter = @"&""微软雅黑,常规""第&P页/共&N页";
                                SingleBomSheet.PageSetup.RightFooter = "";
                                //MessageBox.Show("导出完成。");
                            }//判断是否有记录，有记录才能存入

                        }


                        toolStripStatusLabel1.Text = "正在生成总表封面，请稍后。";
                        //------------封面开始------------------------------------------------------
                        //ProStatus.Text = "正在生成“总表封面”。请稍后。。。";
                        int AllBom = 0;

                        for (int j = 1; j <= OutWorkBook.Worksheets.Count; j++)
                        {
                            if (OutWorkBook.Worksheets[j].Name == "总表封面样板")
                            {
                                AllBom = j;
                            }
                        }
                        SingleBomSheet = OutWorkBook.Worksheets[AllBom];//要操作的表的索引。
                        SingleBomSheet.Name = "总表封面";

                        range = SingleBomSheet.Range[SingleBomSheet.Cells[10, 3], SingleBomSheet.Cells[13, 22]]; //项目名称
                        range.ClearContents(); //先把Range内容清除，合并才不会出错
                        range.MergeCells = true;
                        range.Value2 = AllTitle.Text + "汇总表";

                        range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                        range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
                        range.Name = "微软雅黑";
                        range.Font.FontStyle = "常规"; ;
                        range.Font.Size = 36;

                        range = SingleBomSheet.Range[SingleBomSheet.Cells[20, 10], SingleBomSheet.Cells[21, 16]]; //项目名称
                        range.ClearContents(); //先把Range内容清除，合并才不会出错
                        range.MergeCells = true;
                        range.Value2 = "文件共     页";

                        range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                        range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
                        range.Name = "微软雅黑";
                        range.Font.FontStyle = "常规"; ;
                        range.Font.Size = 20;


                        //自定义页眉
                        SingleBomSheet.PageSetup.LeftHeader = @"&""微软雅黑,常规""文件编号: QR/JS-7.3-13";
                        //AllBomSheet.PageSetup.CenterHeader = @"&""微软雅黑,常规""&18" + textBoxAllTitle.Text + "汇总表";
                        SingleBomSheet.PageSetup.RightHeader = @"&""微软雅黑,常规""版本:" + textBoxAllRev.Text;// textBoxBomRev.Text;
                        //自定义页脚
                        SingleBomSheet.PageSetup.LeftFooter = @"编制:" + textBoxcompile.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");
                        SingleBomSheet.PageSetup.CenterFooter = @"审核:" + textBoxCheck.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");
                        SingleBomSheet.PageSetup.RightFooter = @"批准:" + textBoxRatify.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");

                        //-----------封面结束------------------------------------------------------

                        OutWorkBook.Save();//保存工作表
                        OutWorkBook.Close(false, missing, missing);//关闭工作表

                        //生成总表
                        CollectOut(@saveexcel.FileName);
                        
                        //生成PCB输出清单
                        PcbOut(@saveexcel.FileName);


                        toolStripStatusLabel1.Text = "输出完成";

                        MessageBox.Show("输出成功。", "提示");

                    } //判断是否要存入对放框  
                }
                else
                {
                    MessageBox.Show("输出的单个的表，请在编辑料单中输出。", "提示");
                }
            }
        }

        //PCB文件输出
        public void PcbOut(string filename)
        {
            //1  序号
            int SerialColumn = 1;
            //6  PCB板名
            int PcbNameColumn = 2;
            //7  PCB版本
            //int PcbRevColumn = 2;
            //8  PCB尺寸
            int PcbSizeColumn = 3;
            //9  板内日期
            int PcbDateColumn = 5;
            //11 下单数量
            int PcbNumberColumn = 6;
            //10 板层	
            int PcbLayerColumn = 7;
            //12 厚度
            int PcbThicknessColumn = 8;
            //16 备注： PCB软件版本
            int SoftRevColumn = 9;

            statusStrip1.Text = "正在生成PCB清单,请稍后...";

            /*处理两个工作表分别为：料单样板、器件库*/
            //Excel.Workbook workbook = this.Application.Workbooks[1];    //工作簿
            Microsoft.Office.Interop.Excel.Application workbook = new Microsoft.Office.Interop.Excel.Application();//实例化Excel对象
            object missing = System.Reflection.Missing.Value;//获取缺少的object类型值
            //打开指定的Excel文件
            Microsoft.Office.Interop.Excel.Workbook OutWorkBook = workbook.Application.Workbooks.Open(@filename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            OutWorkBook.Application.DisplayAlerts = false;
            Excel.Range range;

            int PcbNum = 0;

            for (int i = 1; i <= workbook.Worksheets.Count; i++)
            {
                if (workbook.Worksheets[i].Name == "PCB输出样板")
                {
                    PcbNum = i;
                }
            }
            //PCB输出样板存副本
            Excel.Worksheet PcbOutSheet = workbook.Worksheets[PcbNum];//要操作的表的索引。
            PcbOutSheet.Name = "PCB输出";

            int idnum = 1; int PCBrow = 4;

            int[] tempcountid = new int[100];
            int count = 0;

            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i) == true)
                {
                    tempcountid[i] = i;
                    count++;
                }
            }


            ////说明只有一行
            //if (16 >= count)
            //{
            //    //设置一下行高
            //    for (int i = 0; i < 24; i++)
            //    {
            //        range = PcbOutSheet.Rows[5, this.missing];
            //        range.Delete(Excel.XlDirection.xlDown);
            //    }
            //    range = PcbOutSheet.Rows[4, this.missing];
            //    range.RowHeight = "100";
            //}

            string tempstr = "总表输出，ID:" + GlobalStrPath + "  初始版本:\n";

            //count = gxonfigcount;
            for (int i = 0; i < count; i++)
            {
                //序号
                PcbOutSheet.Cells[PCBrow, SerialColumn] = idnum; idnum++;
                //PCB板名
                //PCB版本                                     项目名称      加      BOM文件名   加       版本        加  中文名
                PcbOutSheet.Cells[PCBrow, PcbNameColumn] = OutAllBomList[(i * 17) + 0] + OutAllBomList[(i * 17) + 2] + OutAllBomList[(i * 17) + 5] + "(" + OutAllBomList[(i * 17) + 3] + ")";
                tempstr = tempstr + Convert.ToString(i + 1) + "、" + OutAllBomList[(i * 17) + 0] + OutAllBomList[(i * 17) + 2] + OutAllBomList[(i * 17) + 5] + "(" + OutAllBomList[(i * 17) + 3] + ")\n";
                //PCB尺寸
                PcbOutSheet.Cells[PCBrow, PcbSizeColumn] = OutAllBomList[(i * 17) + 6];
                //板内日期
                PcbOutSheet.Cells[PCBrow, PcbDateColumn] = OutAllBomList[(i * 17) + 7];
                //下单数量
                PcbOutSheet.Cells[PCBrow, PcbNumberColumn] = OutAllBomList[(i * 17) + 9];
                //板层数
                PcbOutSheet.Cells[PCBrow, PcbLayerColumn] = OutAllBomList[(i * 17) + 8];
                //厚度
                PcbOutSheet.Cells[PCBrow, PcbThicknessColumn] = OutAllBomList[(i * 17) + 10];
                //软件版本
                //加工工艺
                PcbOutSheet.Cells[PCBrow, SoftRevColumn] = "工艺;" + OutAllBomList[(i * 17) + 11] + "  设计软件：" + OutAllBomList[(i * 17) + 14];

                PCBrow++;
                
            }

            //------------------------------------生一个输出一个更改记录文件并输出------------------------------------------------
           
            XElement xe = XElement.Load(@"C:\BOM\OUT\OutOrderOutTitleREC.xml");
            IEnumerable<XElement> elements1 = from element in xe.Elements("ChangeRecord")
                                              select element;
            //生成新的编号
            //string str = (Convert.ToInt32(elements1.Max(element => element.Attribute("ID").Value)) + 1).ToString("00000");
            XElement people = new XElement(
                   "ChangeRecord", new XAttribute("ID", GlobalStrPath),             //ID  
                    new XElement("Content", tempstr),                               //更改内容
                    new XElement("Description", textBox7.Text),                     //更改备注说明
                    new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))       //日期    
                );
            xe.Add(people);
            xe.Save(@"C:\BOM\OUT\OutOrderOutTitleREC.xml");
            //getXmlInfo();



            //删除多余的行
            int tempdel = (30 - PCBrow);
            int temprow = PCBrow;
            for (int i = 1; i < tempdel; i++)
            {
                range = PcbOutSheet.Rows[temprow, missing];
                range.Delete(Excel.XlDirection.xlDown);
            }

            //自定义页脚
            PcbOutSheet.PageSetup.LeftFooter = @"编制:" + textBoxcompile.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");
            PcbOutSheet.PageSetup.CenterFooter = @"审核:" + textBoxCheck.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");
            PcbOutSheet.PageSetup.RightFooter = @"批准:" + textBoxRatify.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");

            
            //---------------------------------------生成总表更改记录------------------------------
            for (int i = 1; i <= workbook.Worksheets.Count; i++)
            {
                if (workbook.Worksheets[i].Name == "更改记录样板")
                {
                    PcbNum = i;
                }
            }
            
            PcbOutSheet = workbook.Worksheets[PcbNum];//要操作的表的索引。
            PcbOutSheet.Name = "更改记录";

            range = PcbOutSheet.Columns[2, missing];
            range.ColumnWidth = "80";

            range = PcbOutSheet.Columns[3, missing];
            range.ColumnWidth = "50";

            range = PcbOutSheet.Columns[4, missing];
            range.ColumnWidth = "20";

            PcbOutSheet.Cells[1, 1] = "序号";
            PcbOutSheet.Cells[1, 2] = "更改记录";
            PcbOutSheet.Cells[1, 3] = "备注说明";
            PcbOutSheet.Cells[1, 4] = "日期";
            PcbOutSheet.Cells[1, 5] = "";
            PcbOutSheet.Cells[1, 5] = "";
            PcbOutSheet.Cells[1, 6] = "";
            PcbOutSheet.Cells[1, 7] = "";
            PcbOutSheet.Cells[1, 8] = "";
            PcbOutSheet.Cells[1, 9] = "";
            PcbOutSheet.Cells[1, 10] = "";
            PcbOutSheet.Cells[1, 11] = "";
            PcbOutSheet.Cells[1, 12] = "";
            PcbOutSheet.Cells[2, 1] = "1";
            PcbOutSheet.Cells[2, 2] = tempstr;
            PcbOutSheet.Cells[2, 3] = textBox7.Text;
            PcbOutSheet.Cells[2, 4] =  DateTime.Now.ToString("yyyy-MM-dd");

            int rows1 = 3;
            for (int i = 0; i < count; i++)
            {
                if (File.Exists(OutAllBomList[(i * 17) + 16]))//更改记录 
                {
                    DataSet myds = new DataSet();
                    myds.ReadXml(OutAllBomList[(i * 17) + 16]);
                    if (myds.Tables.Count > 0)
                    {
                        dataGridView1.DataSource = myds.Tables[0];

                        for (int rows = 0; rows < dataGridView1.Rows.Count - 1; rows++)
                        {
                            PcbOutSheet.Cells[rows1,2] = dataGridView1.Rows[rows].Cells[0].Value.ToString();//更改内容
                           PcbOutSheet.Cells[rows1,3]  = dataGridView1.Rows[rows].Cells[1].Value.ToString();//备注
                           PcbOutSheet.Cells[rows1,4]  = dataGridView1.Rows[rows].Cells[2].Value.ToString();//日期
                           PcbOutSheet.Cells[rows1, 1] = dataGridView1.Rows[rows].Cells[3].Value.ToString();//ID
                           rows1++;
                         }
                    }
                    else
                    {
                        dataGridView1.DataSource = null;
                    }   
                }
            }

            //---------------------------------------生成总表更改记录结束------------------------------

            OutWorkBook.Save();//保存工作表
            OutWorkBook.Close(false, missing, missing);//关闭工作表
        }

        //创建一个XML文件
        private void button11_Click(object sender, EventArgs e)
        {
           //string OutID = DateTime.Now.ToString(); // 2008-9-4 20:02:10
            //生成新的编号
            //string str = (Convert.ToInt32(elements1.Max(element => element.Attribute("ID").Value)) + 1).ToString("00000");

            XDocument doc = new XDocument(
                                 new XDeclaration("1.0", "utf-8", "yes"),
                                 new XElement("OrderOutS",                               //定单输出 数据库
                                 new XElement("OrderOut",                                //定单输出 一个定单
                                 new XAttribute("ID", "00001"),                           //ID
                                 new XElement("OutID", "00001"),                          //定单序号
                                 new XElement("ProName", "L1305"),                       //项目名称
                                 new XElement("PrintDate", "2013-07-16"),                 //打印日期
                                 new XElement("BomFileName", "KZB"),                     //BOM表名
                                 new XElement("BomChinaName", "机车信号控制板"),         //BOM中文名
                                 new XElement("PcbName", "KZB"),                         //PCB板名
                                 new XElement("PcbRev", "V10"),                          //PCB版本
                                 new XElement("PcbSize", "150*120"),                     //PCB尺寸
                                 new XElement("PcbDate", "2013-07-16"),                  //PCB板内日期
                                 new XElement("PcbLayer", "4"),                          //PCB层数
                                 new XElement("PcbNumber", "10"),                        //PCB下单数量
                                 new XElement("PcbThickness", "1.6MM"),                  //PCB厚度
                                 new XElement("PcbCraft", "无铅喷纯锡"),                 //PCB工艺
                                 new XElement("PcbOutDate", "2014-05-06"),               //PCB加工日期
                                 new XElement("PcbPath", @"C:\Test.pcb"),                //PCB路径
                                 new XElement("PcbSoft", "protel99se"),                  //PCB设计软件
                                 new XElement("ReserveOne", "备用1"),                    //改为料单料路径
                                 new XElement("ReserveTwo", "备用2")                     //PCB设计软件
                                 )
                                 )
                               );
            doc.Save(@"C:\BOM\OUT\OutOrderOutS.xml");


            XDocument doc1 = new XDocument(
                     new XDeclaration("1.0", "utf-8", "yes"),
                     new XElement("OrderOutS",                               //定单输出 数据库
                     new XElement("OrderOut",                                //定单输出 一个定单
                     new XAttribute("ID", "00001"),                           //ID
                     //new XElement("OutID", OutID),                             //定单序号
                     new XElement("ProTitle", "(样板)L1305机车信号"),         //总表标题
                     new XElement("ProRev", "(样板)V10"),                 //总表版本
                     new XElement("PrintDate", "2014-07-01"),              //打印日期
                     new XElement("ProCompile", "(样板)何政"),             //编制
                     new XElement("ProCheck", "(样板)林世春"),                //审核
                     new XElement("ProRatify", "(样板)牛意坚"),                 //批准
                     new XElement("ProNote", "(样板)机车信号，生产300，")    //输出说明
                     )
                     )
                   );
            doc1.Save(@"C:\BOM\OUT\OutOrderOutTitle.xml");


            XDocument doc2 = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XElement("ChangeRecordS",                                 //定单输出 数据库
                    new XElement("ChangeRecord",                                  //定单输出 一个定单
                    new XAttribute("ID", "00001"),                                 //ID                                   
                    new XElement("Content", "首个记录"),                          //更改内容
                    new XElement("Description", "建立文件的首个记录"),            //更改备注说明
                    new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))     //日期
                    )
                    )
                );
            doc2.Save(@"C:\BOM\OUT\OutOrderOutTitleREC.xml");

            //-------------------------------新增料单更改记录内容-------------------------------------

            ////生成更改记录
            //if (File.Exists(Form1.GlobalBomRecName))
            //{  //已存在，就添加记录
            //    XElement xe = XElement.Load(Form1.GlobalBomRecName);
            //    IEnumerable<XElement> elements1 = from element in xe.Elements("ChangeRecord")
            //                                      select element;
            //    //生成新的编号
            //    string str = (Convert.ToInt32(elements1.Max(element => element.Attribute("ID").Value)) + 1).ToString("00000");

            //    XElement people = new XElement(
            //           "ChangeRecord", new XAttribute("ID", str),
            //            new XElement("Content", tempstr),                             //更改内容
            //            new XElement("Description", tempstrbeizhu),                   //更改备注说明
            //            new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))     //日期
            //        );
            //    xe.Add(people);
            //    xe.Save(Form1.GlobalBomRecName);
            //}//不存在，新创建文件后再加一条记录
            //else
            //{
            //    XDocument doc = new XDocument(
            //            new XDeclaration("1.0", "utf-8", "yes"),
            //            new XElement("ChangeRecordS",                                 //定单输出 数据库
            //            new XElement("ChangeRecord",                                  //定单输出 一个定单
            //            new XAttribute("ID", "00001"),                                 //ID                                   
            //            new XElement("Content", tempstr),                             //更改内容
            //            new XElement("Description", tempstrbeizhu),                   //更改备注说明
            //            new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))     //日期
            //            )
            //            )
            //        );

            //    doc.Save(Form1.GlobalBomRecName);
            //}

            //-------------------------------新增料单更改记录内容结束-----------------------------------


        }


       //        SingleBomConfig = true;
                    //button6.Enabled = true;

                    //GlobalSingleBomConfig[0] = element.Element("ProName").Value;
                    //GlobalSingleBomConfig[1] = element.Element("PrintDate").Value;
                    //GlobalSingleBomConfig[2] = element.Element("BomFileName").Value;
                    //GlobalSingleBomConfig[3] = element.Element("BomChinaName").Value;
                    //GlobalSingleBomConfig[4] = element.Element("PcbName").Value;
                    //GlobalSingleBomConfig[5] = element.Element("PcbRev").Value;
                    //GlobalSingleBomConfig[6] = element.Element("PcbSize").Value;
                    //GlobalSingleBomConfig[7] = element.Element("PcbDate").Value;
                    //GlobalSingleBomConfig[8] = element.Element("PcbLayer").Value;
                    //GlobalSingleBomConfig[9] = element.Element("PcbNumber").Value;
                    //GlobalSingleBomConfig[10] = element.Element("PcbThickness").Value;
                    //GlobalSingleBomConfig[11] = element.Element("PcbCraft").Value;
                    //GlobalSingleBomConfig[12] = element.Element("PcbOutDate").Value;
                    //GlobalSingleBomConfig[13] = element.Element("PcbPath").Value;
                    //GlobalSingleBomConfig[14] = element.Element("PcbSoft").Value;
                    //GlobalSingleBomConfig[15] = element.Element("ReserveOne").Value;
                    //GlobalSingleBomConfig[16] = element.Element("ReserveTwo").Value;


        //private void LoadData(string idcard)
        //{
        //    string xmlFilePath = "Employee.xml";//xml文件存放的路径
        //    XElement xes = XElement.Load(xmlFilePath);//加载xml文件
        //    if (idcard == "")
        //    {
        //        //查询所有的元素
        //        var elements = from ee in xes.Elements("Person")
        //                       select new
        //                       {
        //                           姓名 = ee.Element("Name").Value,
        //                           性别 = ee.Element("Sex").Value,
        //                           年龄 = ee.Element("Age").Value,
        //                           身份证号 = ee.Attribute("IDCard").Value
        //                       };
        //        dataGridView1.DataSource = elements.ToList();
        //    }
        //    else
        //    {
        //        //查询指定名称的元素
        //        var elements = from ee in xes.Elements("Person")
        //                       where (ee.Element("Name").Value == "李四") && (ee.Element("Sex").Value == "女")//ee.Attribute("IDCard").Value == idcard //&& ee.Attribute("Name").Value == "张三"
        //                       select new
        //                       {
        //                           姓名 = ee.Element("Name").Value,
        //                           性别 = ee.Element("Sex").Value,
        //                           年龄 = ee.Element("Age").Value,
        //                           身份证号 = ee.Attribute("IDCard").Value
        //                       };
        //        dataGridView1.DataSource = elements.ToList();
        //    }
        //}

        //获取列的英文标号

        //数字行列转为字母，只参转26以下的。

        /*数字行列转为字母，转26以上的行和列。*/
        public string GetColumnName(int number)
        {
            int h, l;
            h = number / 26;
            l = number % 26;
            if (l == 0)
            {
                h -= 1;
                l = 26;
            }
            string s = GetLetter(h) + GetLetter(l);
            return s;
        }

        //数字行列转为字母，只参转26以下的。
        private string GetLetter(int number)
        {
            switch (number)
            {
                case 1:
                    return "A";
                case 2:
                    return "B";
                case 3:
                    return "C";
                case 4:
                    return "D";
                case 5:
                    return "E";
                case 6:
                    return "F";
                case 7:
                    return "G";
                case 8:
                    return "H";
                case 9:
                    return "I";
                case 10:
                    return "J";
                case 11:
                    return "K";
                case 12:
                    return "L";
                case 13:
                    return "M";
                case 14:
                    return "N";
                case 15:
                    return "O";
                case 16:
                    return "P";
                case 17:
                    return "Q";
                case 18:
                    return "R";
                case 19:
                    return "S";
                case 20:
                    return "T";
                case 21:
                    return "U";
                case 22:
                    return "V";
                case 23:
                    return "W";
                case 24:
                    return "X";
                case 25:
                    return "Y";
                case 26:
                    return "Z";
                default:
                    return "";
            }
        }

        //显示定单内容
        public void displaytest()
        {
            XElement xes = XElement.Load(@"C:\BOM\OUT\OutOrderOutS.xml");//加载xml文件
            //查询指定名称的元素
            var elements = from element in xes.Elements("OrderOut")
                           where element.Element("OutID").Value == comboBox1.Text
                           select new
                           {
                               定单ID = element.Element("OutID").Value,
                               项目名称 = element.Element("ProName").Value,
                               打印日期 = element.Element("PrintDate").Value,
                               BOM表名 = element.Element("BomFileName").Value,
                               BOM中文名 = element.Element("BomChinaName").Value,
                               PCB板名 = element.Element("PcbName").Value,
                               PCB版本 = element.Element("PcbRev").Value,
                               PCB尺寸 = element.Element("PcbSize").Value,
                               PCB板内日期 = element.Element("PcbDate").Value,
                               PCB层数 = element.Element("PcbLayer").Value,
                               PCB下单数量 = element.Element("PcbNumber").Value,
                               PCB厚度 = element.Element("PcbThickness").Value,
                               PCB工艺 = element.Element("PcbCraft").Value,
                               PCB加工日期 = element.Element("PcbOutDate").Value,
                               PCB路径 = element.Element("PcbPath").Value,
                               软件版本 = element.Element("PcbSoft").Value,
                               BOM路径 = element.Element("ReserveOne").Value,
                               更改记录路径 = element.Element("ReserveTwo").Value,
                               ID = element.Attribute("ID").Value,
                           };

            if (elements.Count() > 0)
            {
                dataGridView1.DataSource = elements.ToList();
                button10.Text = "<-";
                this.Width = 1280;
                this.Height = 615;
                this.FormBorderStyle = FormBorderStyle.FixedDialog;
            }
            else
            {
                MessageBox.Show("没有找到相关的记录。");
            }
        
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           // dataGridView1.Width = 0;
           // dataGridView1.Height = 0;
           // dataGridView1.Hide();
           // MessageBox.Show("测试一下");

            displaytest();
            

            XElement xe = XElement.Load(@"C:\BOM\OUT\OutOrderOutTitle.xml");
                IEnumerable<XElement> elements1 = from PInfo in xe.Elements("OrderOut")
                                                  where PInfo.Attribute("ID").Value == comboBox1.Text
                                                 select PInfo;
                foreach (XElement element in elements1)
                {

                    AllTitle.Text = element.Element("ProTitle").Value;//总表标题
                    textBoxAllRev.Text = element.Element("ProRev").Value;//总表版本
                    dateTimePicker1.Text = element.Element("PrintDate").Value;//打印日期
                    textBoxcompile.Text = element.Element("ProCompile").Value;//编制
                    textBoxCheck.Text = element.Element("ProCheck").Value;//审核
                    textBoxRatify.Text = element.Element("ProRatify").Value;//批准
                    textBox7.Text = element.Element("ProNote").Value;//输出说明
               }

               
        }

        private void button12_Click(object sender, EventArgs e)
        {
            //dataGridView1.Hide();
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {

        }

        //存配置表
        string[,] gconfig = new string[100, 19];
        //表的数量
        int gxonfigcount = 0;

        //PCB文件输出1
        public void PcbOut1(string filename)
        {
            //1  序号
            int SerialColumn = 1;
            //6  PCB板名
            int PcbNameColumn = 2;
            //7  PCB版本
            //int PcbRevColumn = 2;
            //8  PCB尺寸
            int PcbSizeColumn = 3;
            //9  板内日期
            int PcbDateColumn = 5;
            //11 下单数量
            int PcbNumberColumn = 6;
            //10 板层	
            int PcbLayerColumn = 7;
            //12 厚度
            int PcbThicknessColumn = 8;
            //16 备注： PCB软件版本
            int SoftRevColumn = 9;

            statusStrip1.Text = "正在生成PCB清单,请稍后...";

            /*处理两个工作表分别为：料单样板、器件库*/
            //Excel.Workbook workbook = this.Application.Workbooks[1];    //工作簿
            Microsoft.Office.Interop.Excel.Application workbook = new Microsoft.Office.Interop.Excel.Application();//实例化Excel对象
            object missing = System.Reflection.Missing.Value;//获取缺少的object类型值
            //打开指定的Excel文件
            Microsoft.Office.Interop.Excel.Workbook OutWorkBook = workbook.Application.Workbooks.Open(@filename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            OutWorkBook.Application.DisplayAlerts = false;
            Excel.Range range;

            int PcbNum = 0;

            for (int i = 1; i <= workbook.Worksheets.Count; i++)
            {
                if (workbook.Worksheets[i].Name == "PCB输出样板")
                {
                    PcbNum = i;
                }
            }
            //PCB输出样板存副本
            Excel.Worksheet PcbOutSheet = workbook.Worksheets[PcbNum];//要操作的表的索引。
            PcbOutSheet.Name = "PCB输出";

            int idnum = 1; int PCBrow = 4;

            int[] tempcountid = new int[100];
            int count = 0;


            string tempstr = "总表输出，ID:" + GlobalStrPath + "  初始版本:\n";

            count = gxonfigcount;

            for (int i = 0; i < count; i++)
            {
                //序号
                PcbOutSheet.Cells[PCBrow, SerialColumn] = idnum; idnum++;
                //PCB板名
                //PCB版本                                     项目名称      加      BOM文件名   加       版本        加  中文名
                PcbOutSheet.Cells[PCBrow, PcbNameColumn] = OutAllBomList[(i * 17) + 0] + OutAllBomList[(i * 17) + 2] + OutAllBomList[(i * 17) + 5] + "(" + OutAllBomList[(i * 17) + 3] + ")";
                tempstr = tempstr + Convert.ToString(i + 1) + "、" + OutAllBomList[(i * 17) + 0] + OutAllBomList[(i * 17) + 2] + OutAllBomList[(i * 17) + 5] + "(" + OutAllBomList[(i * 17) + 3] + ")\n";
                //PCB尺寸
                PcbOutSheet.Cells[PCBrow, PcbSizeColumn] = OutAllBomList[(i * 17) + 6];
                //板内日期
                PcbOutSheet.Cells[PCBrow, PcbDateColumn] = OutAllBomList[(i * 17) + 7];
                //下单数量
                PcbOutSheet.Cells[PCBrow, PcbNumberColumn] = OutAllBomList[(i * 17) + 9];
                //板层数
                PcbOutSheet.Cells[PCBrow, PcbLayerColumn] = OutAllBomList[(i * 17) + 8];
                //厚度
                PcbOutSheet.Cells[PCBrow, PcbThicknessColumn] = OutAllBomList[(i * 17) + 10];
                //软件版本
                //加工工艺
                PcbOutSheet.Cells[PCBrow, SoftRevColumn] = "工艺;" + OutAllBomList[(i * 17) + 11] + "  设计软件：" + OutAllBomList[(i * 17) + 14];
                PCBrow++;

            }

            //删除多余的行
            int tempdel = (30 - PCBrow);
            int temprow = PCBrow;
            for (int i = 1; i < tempdel; i++)
            {
                range = PcbOutSheet.Rows[temprow, missing];
                range.Delete(Excel.XlDirection.xlDown);
            }

            //自定义页脚
            PcbOutSheet.PageSetup.LeftFooter = @"编制:" + textBoxcompile.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");
            PcbOutSheet.PageSetup.CenterFooter = @"审核:" + textBoxCheck.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");
            PcbOutSheet.PageSetup.RightFooter = @"批准:" + textBoxRatify.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");


            //---------------------------------------生成总表更改记录------------------------------
            for (int i = 1; i <= workbook.Worksheets.Count; i++)
            {
                if (workbook.Worksheets[i].Name == "更改记录样板")
                {
                    PcbNum = i;
                }
            }

            PcbOutSheet = workbook.Worksheets[PcbNum];//要操作的表的索引。
            PcbOutSheet.Name = "更改记录";

            range = PcbOutSheet.Columns[2, missing];
            range.ColumnWidth = "80";

            range = PcbOutSheet.Columns[3, missing];
            range.ColumnWidth = "50";

            range = PcbOutSheet.Columns[4, missing];
            range.ColumnWidth = "20";

            PcbOutSheet.Cells[1, 1] = "序号";
            PcbOutSheet.Cells[1, 2] = "更改记录";
            PcbOutSheet.Cells[1, 3] = "备注说明";
            PcbOutSheet.Cells[1, 4] = "日期";
            PcbOutSheet.Cells[1, 5] = "";
            PcbOutSheet.Cells[1, 5] = "";
            PcbOutSheet.Cells[1, 6] = "";
            PcbOutSheet.Cells[1, 7] = "";
            PcbOutSheet.Cells[1, 8] = "";
            PcbOutSheet.Cells[1, 9] = "";
            PcbOutSheet.Cells[1, 10] = "";
            PcbOutSheet.Cells[1, 11] = "";
            PcbOutSheet.Cells[1, 12] = "";
            PcbOutSheet.Cells[2, 1] = "1";
            PcbOutSheet.Cells[2, 2] = tempstr;
            PcbOutSheet.Cells[2, 3] = textBox7.Text;
            PcbOutSheet.Cells[2, 4] = DateTime.Now.ToString("yyyy-MM-dd");

            int rows1 = 3;
            for (int i = 0; i < count; i++)
            {
                if (File.Exists(OutAllBomList[(i * 17) + 16]))//更改记录 
                {
                    DataSet myds = new DataSet();
                    myds.ReadXml(OutAllBomList[(i * 17) + 16]);
                    if (myds.Tables.Count > 0)
                    {
                        dataGridView1.DataSource = myds.Tables[0];

                        for (int rows = 0; rows < dataGridView1.Rows.Count - 1; rows++)
                        {
                            PcbOutSheet.Cells[rows1, 2] = dataGridView1.Rows[rows].Cells[0].Value.ToString();//更改内容
                            PcbOutSheet.Cells[rows1, 3] = dataGridView1.Rows[rows].Cells[1].Value.ToString();//备注
                            PcbOutSheet.Cells[rows1, 4] = dataGridView1.Rows[rows].Cells[2].Value.ToString();//日期
                            PcbOutSheet.Cells[rows1, 1] = dataGridView1.Rows[rows].Cells[3].Value.ToString();//ID
                            rows1++;
                        }
                    }
                    else
                    {
                        dataGridView1.DataSource = null;
                    }
                }
            }

            //---------------------------------------生成总表更改记录结束------------------------------

            OutWorkBook.Save();//保存工作表
            OutWorkBook.Close(false, missing, missing);//关闭工作表
        }


        //通过定单的型式输出
        private void button12_Click_1(object sender, EventArgs e)
        {
            string partnum, partname, parttype, partfoot;
            int tempexist = 0;
            int count = 0;

            if (comboBox1.Text != "")
            {
                //显示定单内容到DATAGRID1中去
                displaytest();
                //存配置内容
                for (int rows = 0; rows < dataGridView1.Rows.Count - 1; rows++)
                {
                    for (int i = 1; i < 18; i++)
                    {
                        OutAllBomList.Add(dataGridView1.Rows[rows].Cells[i].Value.ToString()); 
                    }
                }
                gxonfigcount = dataGridView1.Rows.Count - 1;
                count = dataGridView1.Rows.Count - 1;

                SaveFileDialog saveexcel = new SaveFileDialog();
                saveexcel.Filter = "Excel文件|*.xlsx";

                if (saveexcel.ShowDialog() == DialogResult.OK)
                {
                       
                        BomNumCount = count; //输出要加入的表数

                        File.Copy(@"C:\BOM\Excel2007样板.xlsx", @saveexcel.FileName, true);

                        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();//实例化Excel对象
                        object missing = System.Reflection.Missing.Value;//获取缺少的object类型值
                        //打开指定的Excel文件
                        Microsoft.Office.Interop.Excel.Workbook OutWorkBook = excel.Application.Workbooks.Open(@saveexcel.FileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                        OutWorkBook.Application.DisplayAlerts = false;
                        Excel.Worksheet SingleBomSheet;
                        Excel.Range range;
                        //复制一个表
                        object Missing = System.Reflection.Missing.Value;                   //获取缺少的object类型值                     
                        


                        OkAllList.Clear();       
                        for (int i = 0; i < count; i++)
                        {
                            int ci = i * 17;
                            //存入表名内容
                            BomFileName[i] = OutAllBomList[ci + 2];
                            toolStripStatusLabel1.Text = "正在生成单表，" + OutAllBomList[ci + 2]  + "请稍后。";

                            //生成单表
                            //生成单表函数
                            DataSet myds = new DataSet();
                            myds.ReadXml(OutAllBomList[ci + 15]);//加载料单路径
                            if (myds.Tables.Count > 0)
                            {
                                dataGridView1.DataSource = myds.Tables[0];

                                int sheetnum = 0;
                                for (int j = 1; j <= OutWorkBook.Worksheets.Count; j++)
                                {
                                    if (OutWorkBook.Worksheets[j].Name == "料单样板")
                                    {
                                        sheetnum = j;
                                    }
                                }
                                SingleBomSheet = OutWorkBook.Worksheets[sheetnum];
                                //写入到要输出的单表
                                if ( i+1 < count)
                                {   //复制一个单表
                                    SingleBomSheet.Copy(Missing, SingleBomSheet);
                                    //要操作的表的索引。 
                                    SingleBomSheet = OutWorkBook.Worksheets[sheetnum + 1];
                                }
                   
                                SingleBomSheet.Name = OutAllBomList[ci + 2];//表名

                                for (int rows = 0; rows < dataGridView1.Rows.Count - 1; rows++)
                                {   
                                    partnum = dataGridView1.Rows[rows].Cells[0].Value.ToString();
                                    partname = dataGridView1.Rows[rows].Cells[2].Value.ToString();
                                    parttype = dataGridView1.Rows[rows].Cells[3].Value.ToString();
                                    partfoot = dataGridView1.Rows[rows].Cells[4].Value.ToString();
                                    //SingleBomSheet.Cells[rows + 3, 1] = partnum;//序号
                                    ////SingleBomSheet.Cells[rows + 3, 2] = dataGridView1.Rows[rows].Cells[1].Value.ToString();//统一编号
                                    //SingleBomSheet.Cells[rows + 3, 3] = partname;//器件名称
                                    //SingleBomSheet.Cells[rows + 3, 4] = parttype;//器件型号
                                    //SingleBomSheet.Cells[rows + 3, 5] = partfoot;//器件封装
                                    //SingleBomSheet.Cells[rows + 3, 6] = dataGridView1.Rows[rows].Cells[5].Value.ToString();//器件数量
                                    //SingleBomSheet.Cells[rows + 3, 7] = dataGridView1.Rows[rows].Cells[6].Value.ToString();//器件编号
                                    ////SingleBomSheet.Cells[rows + 3, 8] = dataGridView1.Rows[rows].Cells[7].Value.ToString();//加工数量
                                    //SingleBomSheet.Cells[rows + 3, 9] = dataGridView1.Rows[rows].Cells[8].Value.ToString();//备注
                                                                        if (partnum != "nc删除")
                                    { SingleBomSheet.Cells[rows + 3, 1] = partnum; }  //序号
                                    //SingleBomSheet.Cells[rows + 3, 2] = dataGridView1.Rows[rows].Cells[1].Value.ToString();//统一编号
                                    if (partname != "nc删除")
                                    { SingleBomSheet.Cells[rows + 3, 3] = partname;}//器件名称                                   
                                    if (parttype != "nc删除")
                                    { SingleBomSheet.Cells[rows + 3, 4] = parttype;}//器件型号                                    
                                    if (partfoot != "nc删除")
                                    {SingleBomSheet.Cells[rows + 3, 5] = partfoot;}//器件封装
                                    if (dataGridView1.Rows[rows].Cells[5].Value.ToString() != "nc删除")
                                    { SingleBomSheet.Cells[rows + 3, 6] = dataGridView1.Rows[rows].Cells[5].Value.ToString(); }//器件数量
                                     if (dataGridView1.Rows[rows].Cells[6].Value.ToString() != "nc删除")
                                     {SingleBomSheet.Cells[rows + 3, 7] = dataGridView1.Rows[rows].Cells[6].Value.ToString();}//器件编号
                                     //SingleBomSheet.Cells[rows + 3, 8] = dataGridView1.Rows[rows].Cells[7].Value.ToString();//加工数量
                                     if (dataGridView1.Rows[rows].Cells[8].Value.ToString() != "nc删除")
                                     {SingleBomSheet.Cells[rows + 3, 9] = dataGridView1.Rows[rows].Cells[8].Value.ToString();}//备注   

                                    //查重复并存入
                                    if (OkAllList.Count > 3)
                                    {
                                        for (int k  = 0; k < OkAllList.Count; )
                                        {
                                            if ((OkAllList[k+1] == partname) && (OkAllList[k+2] == parttype) && (OkAllList[k+3] == partfoot))
                                            {
                                                tempexist = 10;
                                                break; 
                                            }
                                            k = k + 4;
                                        }

                                        if (tempexist != 10)
                                        {
                                            OkAllList.Add(partnum);//序号
                                            OkAllList.Add(partname);//器件名称
                                            OkAllList.Add(parttype);//器件型号
                                            OkAllList.Add(partfoot);//器件封装   
                                            tempexist = 0;
                                        }
                                        tempexist = 0;
                                    }
                                    else
                                    {
                                        OkAllList.Add(partnum);//序号
                                        OkAllList.Add(partname);//器件名称
                                        OkAllList.Add(parttype);//器件型号
                                        OkAllList.Add(partfoot);//器件封装                                    
                                    }
                                }

                                int rowstemp = dataGridView1.Rows.Count - 2;
                                //进行排一下序
                                range = SingleBomSheet.Range[SingleBomSheet.Cells[3, 1], SingleBomSheet.Cells[rowstemp, 9]];
                                range.Sort(
                                             range.Columns[3, missing], Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                                             range.Columns[5, missing], missing, Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                                             range.Columns[4, missing], Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending,
                                             Microsoft.Office.Interop.Excel.XlYesNoGuess.xlNo, missing, missing,
                                             Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns,
                                             Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin,
                                             Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                                             Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal,
                                             Microsoft.Office.Interop.Excel.XlSortDataOption.xlSortNormal);


                                menfun();


                                //暂存分表内容
                                List<string> TempList = new List<string>();
                                string stemp;
                                for (int p = 3; p < 200000; p++)
                                {
                                    range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 1], SingleBomSheet.Cells[p, 1]];
                                    stemp = range.Text.ToString();
                                    if (stemp != "")
                                    {
                                        TempList.Add(stemp);
                                        range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 3], SingleBomSheet.Cells[p, 3]];
                                        stemp = range.Text.ToString(); TempList.Add(stemp);
                                        range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 4], SingleBomSheet.Cells[p, 4]];
                                        stemp = range.Text.ToString(); TempList.Add(stemp);
                                        range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 5], SingleBomSheet.Cells[p, 5]];
                                        stemp = range.Text.ToString(); TempList.Add(stemp);
                                        range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 6], SingleBomSheet.Cells[p, 6]];
                                        stemp = range.Text.ToString(); TempList.Add(stemp);
                                        range = SingleBomSheet.Range[SingleBomSheet.Cells[p, 7], SingleBomSheet.Cells[p, 7]];
                                        stemp = range.Text.ToString(); TempList.Add(stemp);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                //开始排序
                                List<string> TempList2 = new List<string>();
                                for (int ai = 0; ai < GAllSortList.Count; )
                                {
                                    for (int jj = 0; jj < TempList.Count; )
                                    {
                                        if (GAllSortList[ai] == TempList[jj + 1])
                                        {
                                            TempList2.Add(TempList[jj + 0]);
                                            TempList2.Add(GAllSortList[ai + 1]);TempList[jj + 1] = GAllSortList[ai + 1];//TempList[jj + 1]
                                            TempList2.Add(TempList[jj + 2]);
                                            TempList2.Add(TempList[jj + 3]);
                                            TempList2.Add(TempList[jj + 4]);
                                            TempList2.Add(TempList[jj + 5]);
                                        }
                                        jj = jj + 6;
                                    }
                                    ai = ai + 2; ;
                                }

                                //重新填入
                                int ttmp = 0, lentht = TempList2.Count / 6 + 3;
                                for (int lls = 3; lls < lentht; lls++)
                                {
                                    SingleBomSheet.Cells[lls, 1] = TempList2[ttmp * 6 + 0];
                                    SingleBomSheet.Cells[lls, 3] = TempList2[ttmp * 6 + 1];
                                    SingleBomSheet.Cells[lls, 4] = TempList2[ttmp * 6 + 2];
                                    SingleBomSheet.Cells[lls, 5] = TempList2[ttmp * 6 + 3];
                                    SingleBomSheet.Cells[lls, 6] = TempList2[ttmp * 6 + 4];
                                    SingleBomSheet.Cells[lls, 7] = TempList2[ttmp * 6 + 5];
                                    ttmp++;
                                }

                                int ttid = lentht, idd2f = 0;
                                for (int inbom = 0; inbom < TempList.Count; )
                                {
                                    for (int d = 0; d < TempList2.Count; )
                                    {
                                        if (TempList[inbom + 1] == TempList2[d + 1])
                                        {
                                            idd2f = 1;
                                            break;
                                        }
                                        d = d + 6;
                                    }
                                    if (idd2f != 1)
                                    {
                                        idd2f = 0;
                                        SingleBomSheet.Cells[ttid, 1] = TempList[inbom + 0];
                                        SingleBomSheet.Cells[ttid, 3] = TempList[inbom + 1];
                                        SingleBomSheet.Cells[ttid, 4] = TempList[inbom + 2];
                                        SingleBomSheet.Cells[ttid, 5] = TempList[inbom + 3];
                                        SingleBomSheet.Cells[ttid, 6] = TempList[inbom + 4];
                                        SingleBomSheet.Cells[ttid, 7] = TempList[inbom + 5];
                                        ttid++;
                                    }
                                    else
                                    {
                                        idd2f = 0;
                                    }
                                    inbom = inbom + 6;
                                }


                                //重新生成一个新排列序号
                                int ids = 1;
                                for (int k = 3; k < 20000; k++)
                                {
                                    range = SingleBomSheet.Range[SingleBomSheet.Cells[k, 1], SingleBomSheet.Cells[k, 1]];
                                    if (range.Text.ToString() != "")
                                    {
                                        SingleBomSheet.Cells[k, 1] = Convert.ToString(ids); ids++;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                } 

                                //自定义页眉
                                SingleBomSheet.PageSetup.LeftHeader = @"&""微软雅黑,常规""文件编号: QR/JS-7.3-13";
                                //输出项目名称+BOM名+版本+中文名
                                SingleBomSheet.PageSetup.CenterHeader = @"&""微软雅黑,常规""&18" + OutAllBomList[ci + 0] + OutAllBomList[ci + 2] + OutAllBomList[ci + 5] + "(" + OutAllBomList[ci + 3] + ")元件清单";
                                SingleBomSheet.PageSetup.RightHeader = @"&""微软雅黑,常规""版本: V10";// textBoxBomRev.Text;
                                //自定义页脚
                                SingleBomSheet.PageSetup.LeftFooter = @"&""微软雅黑,常规""日期:" + DateTime.Now.ToString("yyyy-MM-dd");
                                SingleBomSheet.PageSetup.CenterFooter = @"&""微软雅黑,常规""第&P页/共&N页";
                                SingleBomSheet.PageSetup.RightFooter = "";
                                //MessageBox.Show("导出完成。");
                            }//判断是否有记录，有记录才能存入

                        }


                        toolStripStatusLabel1.Text = "正在生成总表封面，请稍后。";
                        //------------封面开始------------------------------------------------------
                        //ProStatus.Text = "正在生成“总表封面”。请稍后。。。";
                        int AllBom = 0;

                        for (int j = 1; j <= OutWorkBook.Worksheets.Count; j++)
                        {
                            if (OutWorkBook.Worksheets[j].Name == "总表封面样板")
                            {
                                AllBom = j;
                            }
                        }
                        SingleBomSheet = OutWorkBook.Worksheets[AllBom];//要操作的表的索引。
                        SingleBomSheet.Name = "总表封面";

                        range = SingleBomSheet.Range[SingleBomSheet.Cells[10, 3], SingleBomSheet.Cells[13, 22]]; //项目名称
                        range.ClearContents(); //先把Range内容清除，合并才不会出错
                        range.MergeCells = true;
                        range.Value2 = AllTitle.Text + "汇总表";

                        range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                        range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
                        range.Name = "微软雅黑";
                        range.Font.FontStyle = "常规"; ;
                        range.Font.Size = 36;

                        range = SingleBomSheet.Range[SingleBomSheet.Cells[20, 10], SingleBomSheet.Cells[21, 16]]; //项目名称
                        range.ClearContents(); //先把Range内容清除，合并才不会出错
                        range.MergeCells = true;
                        range.Value2 = "文件共     页";

                        range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                        range.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
                        range.Name = "微软雅黑";
                        range.Font.FontStyle = "常规"; ;
                        range.Font.Size = 20;


                        //自定义页眉
                        SingleBomSheet.PageSetup.LeftHeader = @"&""微软雅黑,常规""文件编号: QR/JS-7.3-13";
                        //AllBomSheet.PageSetup.CenterHeader = @"&""微软雅黑,常规""&18" + textBoxAllTitle.Text + "汇总表";
                        SingleBomSheet.PageSetup.RightHeader = @"&""微软雅黑,常规""版本:" + textBoxAllRev.Text;// textBoxBomRev.Text;
                        //自定义页脚
                        SingleBomSheet.PageSetup.LeftFooter = @"编制:" + textBoxcompile.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");
                        SingleBomSheet.PageSetup.CenterFooter = @"审核:" + textBoxCheck.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");
                        SingleBomSheet.PageSetup.RightFooter = @"批准:" + textBoxRatify.Text + "  日期:" + DateTime.Now.ToString("yyyy-MM-dd");

                        //-----------封面结束------------------------------------------------------

                        OutWorkBook.Save();//保存工作表
                        OutWorkBook.Close(false, missing, missing);//关闭工作表

                        //总表要用到这个序号
                        GlobalStrPath = comboBox1.Text;
                        //生成总表
                        CollectOut(@saveexcel.FileName);
                        
                        //生成PCB输出清单
                        PcbOut1(@saveexcel.FileName);


                        toolStripStatusLabel1.Text = "输出完成";

                        MessageBox.Show("输出成功。", "提示");

                    } //判断是否要存入对放框     
            }
        }
    }
}
